动态限制EdiText只能输入特定字符
来源:互联网 发布:java启动exe程序 编辑:程序博客网 时间:2024/05/22 05:17
如何设置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(){
char[] numberChars[]={'1','2','3','4','5','6','7','8','9','0',};
return numberChars;
}
});
二、设置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;
}
});
et = (EditText) findViewById(R.id.et);
// 方法1:建立一个DigitsKeyListener,然后把它设为你的EditText的KeyListener
DigitsKeyListener numericOnlyListener = new DigitsKeyListener(false,true);
et.setKeyListener(numericOnlyListener);
// 方法2:为EditText设置一个NumberKeyListener,然后重写getAcceptedChars()方法和getInputType()方法
et.setKeyListener(new NumberKeyListener() {
@Override
protected char[] getAcceptedChars() {
return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9', '0' };
}
@Override
public int getInputType() {
// TODO Auto-generated method stub
return android.text.InputType.TYPE_CLASS_PHONE;
}
});
--------------------------------------------------------------------------------------------
第一种可以输入小数。
第二种由于设置了TYPE_CLASS_PHONE所以只能输入整数。且比较灵活。
很多网友可能在开发Android时发现EditText有时候需要限制用户输入的内容,通常我们可以使用正则表达式直接限制,但是Android 已经为我们准备好了EditText的输入类型,这样的比正则要有以下几点优势:
1. 开发更简单,执行速度高效。 2. 输入法默认会根据情况变动,比如说设置为numeric后输入法会自动仅显示数字,不会出现Qwerty中的字母。
下面我们通过EditText的layout xml文件中的相关属性来实现:
1. 密码框属性 android:password="true" 这条可以让EditText显示的内容自动为 星号,输入时内容会在1秒内变成*字样。
2. 纯数字 android:numeric="true" 这条可以让输入法自动变为数字输入键盘,同时仅允许0-9的数字输入
3. 仅允许 android:capitalize="cwj1987" 这样仅允许接受输入cwj1987,一般用于密码验证
下面是一些扩展的风格属性
android:editable="false" 设置EditText不可编辑
android:singleLine="true" 强制输入的内容在单行
android:ellipsize="end" 自动隐藏尾部溢出数据,一般用于文字内容过长一行无法全部显示时。
- 动态限制EdiText只能输入特定字符
- CEdit控件限制只能输入特定字符的方法
- 限制editext输入的字符数不超过140个字符
- HTML 限制文本框只能输入特定字符(比如数字 onkeyup+onafterpaste)
- HTML 限制文本框只能输入特定字符(比如数字 onkeyup+onafterpaste)
- Android 限制textview Edittext 只能输入电话号码和特定字符的方法
- 只能输入数字和字母,特定字符
- 限制Editext输入字节长度
- 自定义Editext控制输入字数限制的文本框Editext
- UITextField限制只能输入数字,不能输入其他字符
- 设置EditText只能输入一些特定的字符
- extField限制只能输入一定长度的字符
- android如何限制只能输入指定的字符
- 限制只能输入小数
- 限制只能输入数字
- Android EditText输入限制最大字符长度 和 限制只能输入数字和字母(包含大小写)
- 代码设置editext只能输入数字和整数以及只能输入两位小数的处理
- Winfrom TextBox限制特定输入
- Android JNI错误--原因和解决办法
- ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn
- Phone状态监听机制
- poj 1837 Balance -DP
- cin、cin.get()、cin.getline()、getline()、gets()函数的用法
- 动态限制EdiText只能输入特定字符
- Android权限
- Spring -- 注解配置Bean
- NGUI ScrollView动态添加和删除对象。
- NYOJ 5 Binary String Matching(数据结构)
- 关于Android中物理按键不响应的可能的一个问题。
- jQuery设置disabled属性与移除disabled属性
- H265 Intro - General Concepts
- java程序一年里一个月有多少天