EditText控件无法代码设置ReadOnly只读
有些网友可能发现Android的EditText控件有个设计bug,就是无法使用Java代码方式设置ReadOnly只读方式,但是Android123提示大家通过xml代码可以设置 android:editable="false" ,相关的setEnabled方法这里不适用,同时android.text.Editable.Factory也无法设置可编辑,该类相关方法只有static Editable.Factory getInstance() 和 Editable newEditable(CharSequence source) 两个。
目前还没有很好的解决方法,EditText控件这种设计缺陷确实比较麻烦,当然Android开发网给大家一个曲线的方法,通过父类TextView的addTextChangedListener(TextWatcher watcher) 方法,设置一个listener监控文字修改后立即恢复原来的内容即可。
android的EditText控件实现只读只需设置三个方法:
editText.setCursorVisible(false);//隐藏光标
editText.setFocusable(false);//失去焦点
editText.setFocusableInTouchMode(false);//虚拟键盘隐藏
如果需记录状态,可以继承EditText类,添加一个属性enable来控制