Android EditText控件完美实现只读(Read-Only/Non-Editable)
2012-07-12 16:54
561 查看
Android EditText控件完美实现只读(Read-Only/Non-Editable)
Posted on 2011 年 09 月 19 日 by cola_tin很多朋友困惑于EditText控件的read-only问题, 包括我. Read-only在这里的定义等同于win32 edit控件的read-only, 即: 无法通过UI更改其中的内容, 但可以选定部分内容, 进行复制.
在早期的sdk, EditText有Editable属性(至于这个属性是否有用, 没有测过, 本人入门较晚, 没使用过早期sdk), 现在这个属性已经deprecated了. 网上有大量关于此问题的内容, 要么是掩耳盗铃式的设成non-focusable, 要么是复杂的TextWatch, 始终没有发现简洁完美的方法. 当初曾被此问题折腾得够呛, 甚至用WebView替代过. 其实只需一行代码就能搞定
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.
我想, 这也应该是官方方法了, 纳闷为啥网上搜不出来这种解决方法.
另外, setOnKeyListener其实也是可以的
et.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { return true; } }); //consume key input et.setInputType(InputType.TYPE_NULL);//禁止输入法
这种方法只经过粗略测试, 无法确定是否与前一种等价. 既然已经有完美方法了, 忘了它吧.
相关文章推荐
- EditText完美实现只读(Read-Only/Non-Editable)
- android之baseAdapter实现listview里控件点击和左滑收藏(个人感觉已完美解决)
- .Net/C# 实现真正的只读属性 (ReadOnly Property)
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- .Net/C# 实现真正的只读属性 (ReadOnly Property)
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- Android布局控件DrawerLayout实现完美侧滑效果
- android EditText 控件无法代码设置ReadOnly 只读
- Android自定义LinearLayout实现左右侧滑菜单,完美兼容ListView、ScrollView、ViewPager等滑动控件
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- Android实现登陆页logo随键盘收放动态伸缩(完美解决键盘弹出遮挡控件的问题)
- .Net/C# 实现真正的只读的 Hashtable 类型的属性 (ReadOnly Hashtable Property)
- EditText控件实现只读
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- android的EditText控件实现只读
- android EditText控件设置只读
- 【android】用EditText控件和Button控件实现一个简易计算器
- Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现