zoukankan      html  css  js  c++  java
  • 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"
    自己主动隐藏尾部溢出数据,它通常用于当文本太长,一行不能全部展示。

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    k8s中文网
    python range用法
    python 日志滚动 分文件
    python 语法
    flask 中文编码解码
    python的杨辉三角
    mysql8.0.4以后修改密码方式变更
    flask学习视频
    oralce的lag和lead函数
    JNI 各类数据类型处理
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4844970.html
Copyright © 2011-2022 走看看