Android的:如何EditText上设置接受的数字和字符? [英] Android : How to set acceptable numbers and characters in EditText?

查看:106
本文介绍了Android的:如何EditText上设置接受的数字和字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在EditText上设置接受字符0123456789和分号。下面是code我使用。

I have to set acceptable characters "0123456789" and "semicolon" in the EditText. Below is the code I'm using.

android:digits="0123456789;"
android:inputType="number|text

与实现的问题是HTC手机,分号不能进入,但在三星和索尼爱立信,分号可以输入。另一个问题是,当我在三星和索尼爱立信进入了分号,分号不能被删除。是否有任何缺失属性在上面code?先谢谢了。

The problem with that implementation is in HTC phones, semicolon can't be entered but in Samsung and Sony Ericsson, semicolon can be entered. Another problem is when I entered semicolon in Samsung and Sony Ericsson, semicolon can't be deleted. Is there any missing property in the above code? Thanks in advance.

推荐答案

Android提供了一种简单的方法,通过修改布局XML并添加一个机器人来编辑文本字段:inputType =文本。这使您可以轻松地创建一些基本的验证像数,小数,电话或电子邮件。但是,没有任何参数的字母数字(即无特殊字符)。要做到这一点,你需要使用的输入滤波器如下图所示,并设置要验证在code表示过滤器等领域。该输入滤波器

Android provides an easy way to edit text fields by modifying the layout xml and adding an android:inputType="text". This lets you easily create some basic validations like numbers, decimal, phone or emails. But there's no parameter for alphanumeric (i.e. no special characters). To do this, you need to use an input filter like below and set the fields you want to validate with that filter in code. This input filter

 InputFilter alphaNumericFilter = new InputFilter() {   
     @Override  
     public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)  
     {  
         for (int k = arg1; k < arg2; k++) {   
             if (!Character.isLetterOrDigit(arg0.charAt(k))) {   
             return ""; 
             }   //the first editor deleted this bracket when it is definitely necessary...
         }
         return null;
     }  
 };   
 mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});   

这篇关于Android的:如何EditText上设置接受的数字和字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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