欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中EditText实现不可编辑解决办法

程序员文章站 2022-04-12 12:34:45
android:editable is deprecated: use an to make it editable android:e...

android:editable is deprecated: use an <edittext> to make it editable
android:editable is deprecated: use inputtype instead

分析:关于edittext控件的read-only问题,即: 无法通过ui更改其中的内容, 但可以选定部分内容, 进行复制.在早期的sdk, edittext有editable属性, 现在这个属性已经deprecated了.
 
解决方法:
其实只需一行代码就能搞定et.setkeylistener(null);
注意, 这里不是setonkeylistener, 而是setkeylistener. 此方法是textview的成员, 调用后的效果完全符合预期, 并且获得焦点后不会弹出输入法.
 
下面是官方文档的解释

sets the key listener to be used with this textview. this can be null to disallow user input. note that this method has significant and subtle interactions with soft keyboards and other input method: see keylistener.getcontenttype() for important details. calling this method will replace the current content type of the text view with the content type returned by the key listener.
 
be warned that if you want a textview with a key listener or movement method not to be focusable, or if you want a textview without a key listener or movement method to be focusable, you must call setfocusable again after calling this to get the focusability back the way you want it.