有没有办法以编程方式选择 TextView 中的文本? [英] Is there any way to programatically select text in a TextView?

查看:38
本文介绍了有没有办法以编程方式选择 TextView 中的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TextView,我希望它允许用户搜索特定的字符串.如果找到字符串,它应该突出显示.使用背景跨度太慢而且笨拙,所以我想弄清楚是否可以让它选择字符串.我知道使用 EditText 这可以使用 setSelection(),但我不希望用户能够编辑文本,同时仍然能够手动突出显示文本,我似乎无法使用 EditText 进行管理.

I have a TextView that I'm looking to allow the user to search in for a specific string. If the string is found, it should highlight. Using a background span is too slow and awkward, so I am trying to figure out if I can just have it select the string. I know with EditText this would be possible using setSelection(), but I don't want the user to be able to edit the text, while still being able to manually highlight text, which I can't seem to do manage with an EditText.

我想,那么它是一个要么或;是否可以以编程方式在 TextView 中选择文本允许文本选择而不允许在 EditText?

I guess, then it's an either or; is it either possible to programmatically select text in a TextView or possible to allow text selection without allowing editing in an EditText?

注意:我实际上使用的是扩展 TextView 的自定义视图,所以我假设它是那个或扩展 EditText;我只是不确定哪个(如果有的话)会起作用.

Note: I'm actually using a custom view extending TextView, so I'm assuming it's either that or extend EditText; I'm just not sure which (if either) will work.

推荐答案

不确定问题是否仍然存在,我会提供我的解决方案.也许对来自搜索引擎的人有用.

Not sure whether the question is still actual, I will provide my solution. Maybe will be useful for people coming from search engines.

所以,据我所知,目的是选择 TextView 中的所有文本,而不能修改其内容.我没有检查它对非常大的文本的效果如何,但希望不会那么糟糕.

So the purpose, as I understood, is to select all text in TextView without being able to modify its content. I didn't check how effective it is against very large text, but hope that not so bad.

请注意,API 版本应 >=11

Please note, API version should be >=11

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;

public class SelectableTextView extends TextView
{
    public SelectableTextView(Context context)
    {
        super(context);
        init();
    }

    public SelectableTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SelectableTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
    {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init()
    {
        if (Build.VERSION.SDK_INT > 10)
            setTextIsSelectable(true);
    }

    @Override
    public boolean onTextContextMenuItem(int id)
    {
        switch (id)
        {
            case android.R.id.cut:
                return true;

            case android.R.id.paste:
                return true;

            case android.R.id.shareText:
            {
                String selectedText = getText().toString().substring(getSelectionStart(), getSelectionEnd());

                if (selectedText != null && !selectedText.isEmpty())
                {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    sendIntent.putExtra(Intent.EXTRA_TEXT, selectedText);
                    sendIntent.setType("text/plain");
                    sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getContext().startActivity(sendIntent);
                }

                return true;
            }

            case android.R.id.selectAll:
            {
                selectAllText();
                return true;
            }
        }

        return super.onTextContextMenuItem(id);
    }

    public void selectAllText()
    {
        if (Build.VERSION.SDK_INT > 10)
            Selection.setSelection((Spannable) getText(), 0, length());
    }

}

这篇关于有没有办法以编程方式选择 TextView 中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆