android代码集EditText只要输入号码、信
如何设置EditText,因此,只有某些数字或字母可以进入它?
一、建立EditText,只要输入号码:
办法1:直接生成DigitsKeyListener了。
et_1.setKeyListener(new
DigitsKeyListener(false,true));
方法2:在EditText中设置属性。android:numeric="integer"即仅仅能输入整数。例如以下
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;
}
});
EditText et;et = (EditText) findViewById(R.id.et);// 方法1:建立一个DigitsKeyListener,然后把它设为你的EditText的KeyListenerDigitsKeyListener numericOnlyListener = new
DigitsKeyListener(false,true);et.setKeyListener(numericOnlyListener);//
方法2:为EditText设置一个NumberKeyListener,然后重写getAcceptedChars()方法和getInputType()方法et.setKeyListener(new NumberKeyListener() {@Overrideprotected char[] getAcceptedChars() {return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9', '0'
};}@Overridepublic int getInputType() {// TODO Auto-generated method stubreturn android.text.InputType.TYPE_CLASS_PHONE;}});
--------------------------------------------------------------------------------------------
01.EditText et;02.et = (EditText) findViewById(R.id.et);03.// 方法1:建立一个DigitsKeyListener,然后把它设为你的EditText的KeyListener04.DigitsKeyListener numericOnlyListener = new
DigitsKeyListener(false,true);05.et.setKeyListener(numericOnlyListener);06.//
方法2:为EditText设置一个NumberKeyListener,然后重写getAcceptedChars()方法和getInputType()方法07.et.setKeyListener(new NumberKeyListener() {08. @Override09. protected char[] getAcceptedChars() {10. return new char[] { '1', '2', '3', '4', '5', '6', '7', '8','9',
'0' };11. }12. @Override13. public int getInputType() {14. // TODO Auto-generated method stub15. return android.text.InputType.TYPE_CLASS_PHONE;16. }17.});
小结:
第一种能够输入小数。
另外一种因为设置了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。一般用于password验证
以下是一些扩展的风格属性
android:editable="false"
设置EditText不可编辑
android:singleLine="true"
强制输入的内容在单行
android:ellipsize="end"
自己主动隐藏尾部溢出数据,它通常用于当文本太长,一行不能全部展示。
版权声明:本文博主原创文章,博客,未经同意不得转载。
android代码集EditText只要输入号码、信的更多相关文章
- android代码设置EditText只输入数字、字母
如何设置EditText,使得只能输入数字或者某些字母呢? 一.设置EditText,只输入数字: 方法1:直接生成DigitsKeyListener对象就可以了. et_1.setKeyLis ...
- Android中设定EditText的输入长度(转)
如何限定Android的Text中的输入长度呢? 方法一:可以在layout xml中加上属性android:maxLength 比如: <EditText android:id ...
- Android中设定EditText的输入长度
方法一:可以在layout xml中加上属性android:maxLength 比如: <EditText android:id="@+id/editTextShow& ...
- Android TextWatcher监控EditText中的输入内容并限制其输入字符个数
布局中EditText在android布局中经常用到,对EditText中输入的内容也经常需要进行限制,我们可以通过TextWatcher去观察输入框中输入的内容,作个笔记. 主布局: <?xm ...
- 【转】android 中如何限制 EditText 最大输入字符数
原文网址:http://blog.csdn.net/fulinwsuafcie/article/details/7437768 方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:andr ...
- android 中如何限制 EditText 最大输入字符数
方法一: 在 xml 文件中设置文本编辑框属性作字符数限制 如:android:maxLength="10" 即限制最大输入字符个数为10 方法二: 在代码中使用InputFilt ...
- Android中EditText设置输入条件
一.应用场景 之前做商城应用时,会有对用户资料的设置情况进行限制,如下: (1)用户邮箱,应当只允许输入英文字母,数字和@.两个符号, (2)用户手机,应当只能输入数字,禁止输入其他字符. (3)用户 ...
- Android 实现限制EditText输入文字的数量
前段时间比较忙 没来的及时分享出来.需求是这样的要求用户只能输入12个字符或者6位中文的数据:作为一个菜鸟遇到这样的问题第一反应就是 Android:maxLength="12"这 ...
- Android限定EditText的输入类型为数字或者英文(包括大小写),EditText,TextView只能输入两位小数
Android限定EditText的输入类型为数字或者英文(包括大小写) // 监听密码输入框的输入内容类型,不可以输入中文 TextWatcher mTextWatcher = new Tex ...
随机推荐
- coco2d-x CCScrollView实现背包翻页,仅供参考
#include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; CCCGameScrollView::CCCGameScr ...
- PHP实现冒泡排序、双向冒泡排序算法
冒泡排序(Bubble Sort),是一种较简单的.稳定的排序算法.冒泡排序算法步骤:比较相邻的元素,如果第一个比第二个大,就交换他们两个的位置:对每对相邻的元素执行同样的操作,这样一趟下来,最后的元 ...
- 初识Mongodb之[CURD]-PHP版
行动 在了实践之前,希望大家看一下上面的学习资源,了解一下基本操作. 数据连接初始账号password 账号:admin password:admin 首先我们建立一个文件:mongodb.php,设 ...
- Webserver管理系列:1、安装Windows Server 2008
简单了解下server: 1U: 2U: 3U: 在安装Windows Server 2008之前我们先了解下Windows Server 2008: Windows Server 2008是微软一个 ...
- 分布式Unique ID的生成方法
分布式Unique ID的生成方法 分布式的Unique ID的用途如此广泛,从业务对象Id到日志的TraceId,本文总结了林林总总的各种生成算法. 1. 发号器 我接触的最早的Unique ID, ...
- 讨论asp.net通过机器cookie仿百度(google)实现搜索input搜索提示弹出框自己主动
为实现自己主动弹出通过用户输入关键词相关的搜索结果,在这里,我举两个解决方案,对于两个不同的方案. 常用的方法是建立一个用户数据库中查找关系表.然后输入用户搜索框keyword异步调用数据表中的相关数 ...
- 2012天津E题
给我们n个坐标点和一个距离d,表示车开m米就没油了. 然后要选几个点建立油站,使得能够从1出发遍历所有的点,然后回到1. 并且规定1这个点必须有油站,在第i个点建立油站的费用是 2^(i-1) 因为 ...
- 让UIView窄斜
让UIView窄斜 by 吴雪莹 [UIView animateWithDuration:0.5 animations:^{ CGAffineTransform t = CGAffineTransfo ...
- MySQL的create table as 与 like区别(转)
对于mysql的复制相同表结构方法,有create table as 和create table like 两种,区别是什么呢? create table t2 as select * from t1 ...
- PHP --字符串编码转换(自动识别原编码)
/** * 对数据进行编码转换 * @param array/string $data 数组 * @param string $output 转换后的编码 */ function array_icon ...