如何允许用户在 TextView 中选择文本范围(类似于 EditText) [英] How to allow the user to select a text range in a TextView (Similar to EditText)
问题描述
我有一个 TextView
,我想让用户从中选择一个文本范围.TextView
占据了设备的整个宽度和高度(减去一些填充和顶部的标题).在 EditText
中,如果您长按,您会得到一个选择覆盖,允许您设置选择的左右边界.我想在 TextView
中使用这个功能.我读过 API 级别 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) 有新的文本选择控件,但我很难实现这一点.我现在正在这样做:
I've got a TextView
that I would like to allow the user to select a range of text from within it. The TextView
takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText
if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView
. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:
eic = new InputConnection( bookTextView );
eic.beginBatchEdit();
但它并没有做任何值得注意的事情.有谁知道如何正确使用 InputConnection
?谢谢.
But it doesn't do anything noticable. Does anyone know how to use InputConnection
correctly? Thanks.
我不一定需要使用我在上面尝试的内容.我最终想使用一个 TextView
或一个 EditText
,它看起来和感觉就像一个 TextView
并且能够使用拖动光标选择文本.然后我想使用各种上下文菜单选项(或在所选文本上方弹出的菜单)来操作所选文本.
I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView
or an EditText
which looks and feels like a TextView
and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).
推荐答案
这里有个idea..添加一个带有TextView背景的EditText,这里是一个例子
Here is an idea.. Add an EditText with a TextView background, Here is an example
<EditText
android:text=" This is not an editable EditText"
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "@android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "@android:drawable/dark_header">
</EditText>
将它添加到您的 xml 中代替 TextView
add this to your xml in the place of TextView
这篇关于如何允许用户在 TextView 中选择文本范围(类似于 EditText)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!