EditView不可编辑状态和可编辑状态动态切换 及 EditView的其它特效
2016-05-25 12:08
309 查看
不可编辑状态:
两种选择:
第一个 不可编辑但是会弹出输入法
android:editable="false"
第二个 不可编辑同时不会弹出输入法
android:focusable="false"
代码动态控制:
editText.setFocusable(false);和editText.setEnabled(false);时不可编辑,但是editText.setFocusable(true);和 editText.setEnabled(true);也是不可编辑的,感觉这个时候EditText控件高亮度了,但是没有焦点
最后尝试使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);设置不可编辑状态;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();设置可编辑状态
这个可以实现可编辑和不可编辑,但是又发现一个问题,在不可编辑状态如果常按住控件,可以进行粘帖操作,不知道怎么可以不能进行粘帖
经测试发现,如果EditText预先有内容,光标自然会在文字的末尾,但是如果预先内容为空,然后设置好内容,这种情况下光标自然会在文字的开头,所以这种情况下可以这样做让光标位于末尾:
editText.setText("0");
editText.requestFocus();
android:background="#00000000"//不要文本框背景
EditText字数限制的设置
(1)在.xml中设置:android:maxLength=“50”
如何设置EditText,使得只能输入数字或者某些字母呢?
一、设置EditText,只输入数字:
方法1:直接生成DigitsKeyListener对象就可以了。
et_1.setKeyListener(new DigisKeyListener(false,true));
方法2:在EditText中设置属性,android:numeric="integer"即只能输入整数,如下
<EditText
android:singleLine="true"
android:numeric="integer"
/>
方法3:新建一个char[],在里面添加允许输入的字符。如下
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars(){
return new char[]{'1','2','3','4','5','6','7','8','9','0'};
}
public int getInputType() {
return android.text.InputType.TYPE_CLASS_PHONE;
}
});
二、设置EditText只能输入某些字母,如下面设置edtitext只能输入A—N,a—n这些字母。方法如下:
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars()
{
char[] numberChars[]={'a,'b','c','d','e','f','A','B','C','D'};
return numberChars;
}
});
更多参考:http://www.cnblogs.com/weixing/p/3257058.html
两种选择:
第一个 不可编辑但是会弹出输入法
android:editable="false"
第二个 不可编辑同时不会弹出输入法
android:focusable="false"
代码动态控制:
editText.setFocusable(false);和editText.setEnabled(false);时不可编辑,但是editText.setFocusable(true);和 editText.setEnabled(true);也是不可编辑的,感觉这个时候EditText控件高亮度了,但是没有焦点
最后尝试使用editText.setFocusable(false);和editText.setFocusableInTouchMode(false);设置不可编辑状态;editText.setFocusableInTouchMode(true);editText.setFocusable(true);editText.requestFocus();设置可编辑状态
这个可以实现可编辑和不可编辑,但是又发现一个问题,在不可编辑状态如果常按住控件,可以进行粘帖操作,不知道怎么可以不能进行粘帖
经测试发现,如果EditText预先有内容,光标自然会在文字的末尾,但是如果预先内容为空,然后设置好内容,这种情况下光标自然会在文字的开头,所以这种情况下可以这样做让光标位于末尾:
editText.setText("0");
editText.requestFocus();
android:background="#00000000"//不要文本框背景
EditText字数限制的设置
(1)在.xml中设置:android:maxLength=“50”
如何设置EditText,使得只能输入数字或者某些字母呢?
一、设置EditText,只输入数字:
方法1:直接生成DigitsKeyListener对象就可以了。
et_1.setKeyListener(new DigisKeyListener(false,true));
方法2:在EditText中设置属性,android:numeric="integer"即只能输入整数,如下
<EditText
android:singleLine="true"
android:numeric="integer"
/>
方法3:新建一个char[],在里面添加允许输入的字符。如下
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars(){
return new char[]{'1','2','3','4','5','6','7','8','9','0'};
}
public int getInputType() {
return android.text.InputType.TYPE_CLASS_PHONE;
}
});
二、设置EditText只能输入某些字母,如下面设置edtitext只能输入A—N,a—n这些字母。方法如下:
editText.setKeyListener(new NumberKeyListener(){
protected char[] getAcceptedChars()
{
char[] numberChars[]={'a,'b','c','d','e','f','A','B','C','D'};
return numberChars;
}
});
更多参考:http://www.cnblogs.com/weixing/p/3257058.html
相关文章推荐
- jQuery EasyUI的使用
- Codeforces Round #353 (Div. 2) C. Money Transfers(数学题)
- 如何选择一个 Linux Tracer (2015)
- 十条nmap常用的扫描命令
- 15个值得开发人员关注的jQuery开发技巧和心得总结【经典收藏】
- C++ 右值引用
- Java并发编程:synchronized
- spring mvc 工作流程
- Linux性能优化--CPU[备忘]
- Velocity 模板引擎介绍
- 广度优先算法
- objective-c启用ARC时的内存管理
- sql 资料杂记
- C++ map,set内部数据结构
- HDU 1203 I NEED A OFFER!
- Spring+Mina(六)
- StrutsPrepareAndExecuteFilter中excludedPatterns变量的用法
- SQL Server 2008——再谈主键和外键
- mysql replication inside
- 【结构型】Bridge模式