EditText完美实现只读(Read-Only/Non-Editable)
来源:互联网 发布:九品网络电视 是什么 编辑:程序博客网 时间:2024/06/05 14:11
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 inputet.setInputType(InputType.TYPE_NULL);//禁止输入法
看到这个问题大家可能有点奇怪了,EditText的功能不就是往上面写入内容吗?再者,如果真要禁止输入文本,在布局文件中添加android:focusable="false",或者在代码中使用editText.setFocusable(false),不就Ok了?
项目需求是这样的,如果EditText上面已经被setText()内容,则需要禁止输入,防止它被修改。
如果没有显示内容,则将EditText设置为可输入状态。
经过测试验证:setFocusable方法的效果只有第一次使用时有效,也就是说若在布局文件里面设置:
android:focusable="false",即使你在代码中设置此控件属性:editText.setFocusable(true);也不能对它进行编辑。
即setFocusable方案不可行。经过摸索得出可行方案。
利用 editText.setInputType(InputType.TYPE_NULL);来禁止手机软键盘。
editText.setInputType(InputType.TYPE_CLASS_TEXT);来开启软键盘。
应用程序默认为开启状态。
特别注意:这种方法也只能禁止软键盘,若手机自带硬键盘,此方案失效。
public class EditTextTest extends Activity{/** test EditText forbid input function demo */EditText editText;boolean flag = true;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);editText = (EditText) findViewById(R.id.editText1);Button btn = (Button) findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener(){public void onClick(View v){if (flag==true){System.out.println("开启软键盘"); editText.setInputType(InputType.TYPE_CLASS_TEXT);flag = false;}else{System.out.println("禁止软键盘"); editText.setInputType(InputType.TYPE_NULL);flag = true;}}});}}
- EditText完美实现只读(Read-Only/Non-Editable)
- Android EditText控件完美实现只读(Read-Only/Non-Editable)
- SET TRANSACTION READ ONLY 只读事务
- Oracle Read-only Tablespace(只读表空间)
- EditText控件实现只读
- 只读表(Read-only)--Oracle 11g 新特性
- 如何快速解决linux只读系统 Read-only file system
- spring 事务管理属性为只读read-only="true"是什么意思
- Linux文件系统只读Read-only file system的解决方法
- spring事务read-only只读报错处理总结
- Linux文件系统只读Read-only file system的快速解决方法
- Spring read-only="true" 只读事务的一些概念
- adb remount 系统提示只读文件系统Read-only file system
- Spring read-only="true" 只读事务的一些概念
- EditText focusable editable enable
- android的EditText控件实现只读
- Oracle 11g 新特性 -- 只读表(read-only table)说明
- Oracle 11g 新特性 -- 只读表(read-only table)
- 第六章——数字签名
- 黑马程序员之C#学习笔记: new关键字的三种用法
- 我的学习计划
- Python alternatives for PHP functions
- android腾讯微博吹一吹实现原理
- EditText完美实现只读(Read-Only/Non-Editable)
- linux下查找某文件的find 命令
- DBMS_STATS.GATHER_TABLE_STATS详解 Oracle统计值
- AVL树的插入、删除、旋转
- DSP的数字滤波器的实现
- JS try.....catch的使用
- 常见字符串操作: C++ string 类 和Java String类 用法小结
- 触发器
- 设计模式