本文介紹了如何在 Android 中使用 InputFilter 限制 EditText 中的字符?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我只想將字符限制為 0-9、a-z、A-Z 和空格鍵.設置 inputtype 我可以限制為數字,但我無法弄清楚 Inputfilter 瀏覽文檔的方式.
I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the docs.
推薦答案
我在另一個論壇上找到了這個.像冠軍一樣工作.
I found this on another forum. Works like a champ.
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[] { filter });
這篇關于如何在 Android 中使用 InputFilter 限制 EditText 中的字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!