How to make an EditText selectable but not editable on Android Ice Cream Sandwich?

ⅰ亾dé卋堺 提交于 2019-12-20 17:30:02

问题


I have a ListView in Activity, and in each item , I insert an EditText for show my text. I need drag the handle to select text & copy the text, but can not edit text. On Android ICS, How can I do this?


回答1:


text.setTextIsSelectable(true) requires API 11. For those using lower API's: In xml use:

android:inputType="none"
android:textIsSelectable="true"

This will make your EditText uneditable but selectable.




回答2:


I think you could override the onKeyDown() method, make it do nothing but return true. It should catch the keypresses from the keyboard and negate them before they can go into the text in the EditText.

You might also try setting editable to false like this

android:editable="false"

on the EditText in your xml layout. However I am not certain if this will let you still highlight and copy, but it is worth a shot.




回答3:


I resolved this problem.
Just set TextView like this:

text.setTextIsSelectable(true);



回答4:


Althouth android:editable is deprecated and Lint suggests to use inputType, It doesn't seem to work. So android:editable="false"is apparently the best possible solution for all android versions.

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:editable="false"
    android:textIsSelectable="true"
    tools:ignore="Deprecated" />


来源:https://stackoverflow.com/questions/10544315/how-to-make-an-edittext-selectable-but-not-editable-on-android-ice-cream-sandwic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!