How to allow the user to select a text range in a TextView (Similar to EditText)

后端 未结 5 702
闹比i
闹比i 2020-12-08 05:27

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 heigh

相关标签:
5条回答
  • 2020-12-08 06:05

    You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.

    0 讨论(0)
  • 2020-12-08 06:11

    Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My idea is the same as sandy's.

    My code is here, hope it may help you:

    https://stackoverflow.com/a/11026292/966405

    0 讨论(0)
  • 2020-12-08 06:15

    After long internet surfing to find a solution, i prefered create my own class

    https://github.com/orionsource/SelectableTextViewer

    enter image description here

    Goal features:

    1. Easy to use - only one class
    2. Support for text and Html.fromHtml
    3. Can be in ScrollView with correct touches
    4. Cursors can be redefined
    5. Color of selection can be redefined
    0 讨论(0)
  • 2020-12-08 06:21

    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>
    

    add this to your xml in the place of TextView

    0 讨论(0)
  • 2020-12-08 06:27

    You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.

    See also:

    http://developer.android.com/reference/android/text/Spanned.html

    0 讨论(0)
提交回复
热议问题