如何允许用户选择一个文本范围在一个TextView(类似的EditText)文本、类似、范围、用户

2023-09-12 11:02:30 作者:你的情话你的谎话*

我有一个的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();

不过,这并不做任何事情noticable。有谁知道如何 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).

推荐答案

下面是一个想法。增加一个EditText有一个TextView的背景,下面是一个例子

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>

这在TextView中的位置添加到您的XML

add this to your xml in the place of TextView