問題描述
我必須在 EditText 中設置可接受的字符0123456789"和分號".下面是我正在使用的代碼.
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 手機中,無法輸入分號,但在三星和索尼愛立信中,可以輸入分號.另一個問題是我在三星和索尼愛立信輸入分號時,分號無法刪除.上面的代碼中是否缺少任何屬性?提前致謝.
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 并添加 android:inputType="text" 來編輯文本字段的簡單方法.這使您可以輕松創建一些基本驗證,例如數字、小數、電話或電子郵件.但是字母數字沒有參數(即沒有特殊字符).為此,您需要使用如下所示的輸入過濾器,并在代碼中使用該過濾器設置要驗證的字段.這個輸入過濾器
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 中設置可接受的數字和字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!