如何限制EDITTEXT在Android的某些特殊字符?特殊字符、EDITTEXT、Android

2023-09-05 04:07:01 作者:﹏嬌气er°

searchEdit.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 &安培;));

我用上面的语句来限制我的EditText部分字符,这是很好的工作在我Motorolo(安卓2.3),但它显示的数字键盘在谷歌Nexus。

请提供了解决方案。

解决方案

第一次尝试添加安卓位数=ABCDE ..... 012345789属性。虽然安卓位数指定它是一个数字字段,但它接受信件以及。 或 试试这个code ..

  T1 =(EditText上)findViewById(R.id.text);

    t1.setFilters(新输入过滤器[] {
            新的输入过滤器(){
                公共CharSequence的过滤器(SRC的CharSequence,诠释开始,
                        INT端,跨区DST,INT DSTART,诠释DEND){

                    如果(src.equals()){//退格键
                        返回SRC;
                    }
                    如果(src.toString()。匹配([A-ZA-Z0-9] *))//把你限制在这里
                    {
                        返回SRC;
                    }
                    返回 ;
                }
            }
        });
 
Android EditText输入限制最大字符长度 和 限制只能输入数字和字母 包含大小写

或 看到此链接

searchEdit.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 &"));

I used the above statement for restricting my edittext to some characters,it is working well in my Motorolo(Android 2.3),but it is showing numeric keyboard in Google nexus.

Please provide the solution.

解决方案

First try with adding the android:digits="abcde.....012345789" attribute.Although the android:digits specify that it is a numeric field but it accept letters as well. Or, try this code..

t1 = (EditText)findViewById(R.id.text);

    t1.setFilters(new InputFilter[] {
            new InputFilter() {
                public CharSequence filter(CharSequence src, int start,
                        int end, Spanned dst, int dstart, int dend) {

                    if(src.equals("")){ // for backspace
                        return src;
                    }
                    if(src.toString().matches("[a-zA-Z0-9 ]*")) //put your constraints here
                    {
                        return src;
                    }
                    return "";
                }
            }
        }); 

Or, see an useful example from This Link

 
精彩推荐
图片推荐