如何限制EDITTEXT在Android的某些特殊字符? [英] How to restrict Edittext to some particular characters in android?

查看:352
本文介绍了如何限制EDITTEXT在Android的某些特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<$p$p><$c$c>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;
                    }
                    返回 ;
                }
            }
        });
 


看到此链接

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

这篇关于如何限制EDITTEXT在Android的某些特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆