editText限制输入的5种方法

XML布局文件限制:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
     >

    <!--方法1 xml中配置inputType。-->
   <EditText
       android:id="@+id/et1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="只限数字(xml中 配置inputType)"
       android:inputType="number" />
    <!--方法2 xml中配置digits -->
   <EditText
       android:id="@+id/et2"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:digits=" .@~-,:*?!#/\\=+?^;%$()[]{}|`&lt;&gt;&amp;&quot;&apos;_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLIMNOPQRSTUVWXYZ"
       android:hint="只限自定义特殊字符(xml中  配置digits)"
        />
   <EditText
       android:id="@+id/et3"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="只限abcd(java中使用addTextChangedListener)"
        />
   <EditText
       android:id="@+id/et4"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="只限wxyz(java中使用setFilters)"
        />

</LinearLayout>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

Java代码中限制:

package com.example.editText;

import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.widget.EditText;
/*
 * 方式1:xml中配置inputType。 常用于限制为 Date,time,number,Email,phone等常用的格式
 * 方式2:xml中配置digits。可以自定义限制的区间。
 * 方式3:java中使用setKeyListener,添加DigitsKeyListener。(方法2就是最终就是通过该方法实现)
 * 方法4:java中使用setFilters,添加InputFilter。可以在回调函数filter中自己写处理,最后返回处理过的CharSequence对象。 * 方法5:在xml属性中限制editText的输入长度,,maxLength = 11; 表示限制十一个字符
 * 
 * 方法:java中使用addTextChangedListener,添加 TextWatcher。不可行!!! (具体见最下面注释)
 */
public class MainActivity extends Activity {

    private EditText et3, et4;
    private final static String ET3_DIGITS = "abcd";
    private final static String ET4_DIGITS = "wxyz";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et3 = (EditText) findViewById(R.id.et3);
        et4 = (EditText) findViewById(R.id.et4);

        //方式3:java中使用setKeyListener,添加DigitsKeyListener。
        et3.setKeyListener(DigitsKeyListener.getInstance(ET3_DIGITS));

        //方法4:java中使用setFilters,添加InputFilter。
        et4.setFilters(new InputFilter[] {new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end,
                    Spanned dest, int dstart, int dend) {           
                //source:你即将输入的字符序列
                Log.d("jie","source = "+source);

                //start:默认为0,  end:你即将输入的字符序列的长度
                Log.d("jie","start = "+ start);
                Log.d("jie","end = "+ end);

                //dest:当前EditText显示的内容
                Log.d("jie","dest = "+dest);

                //经测试dstart和dend 总是相等,都表示输入前光标所在位置
                Log.d("jie","dstart = "+dstart);
                Log.d("jie","dend = "+dend);

                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < source.length(); i++) {
                    if (ET4_DIGITS.indexOf(source.charAt(i)) >= 0) {
                        sb.append(source.charAt(i));
                    }
                }
                return sb;
            }
        }});


        //使用TextWatcher,虽然能够检测到text 内容修改,但是不能进行重新赋值,因为你如果重新setText,又会重新触发该监听,会形成死循环,最终导致栈溢出。故该方法不可行
       /*et3.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < s.length(); i++) {
                    if (ET3_DIGITS.indexOf(s.charAt(i)) >= 0) {
                        sb.append(s.charAt(i));
                    }
                }
                et3.setText(sb);
            }
        });*/

    }

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值