Android EditText禁止輸入空格和特殊字符
有時(shí)候我們需要限制EditText輸入的字符類型,如空格,特殊字符等,這時(shí)候我們可以使用系統(tǒng)提供的輸入過濾器——InputFilter。具體實(shí)現(xiàn)如下:
/**
* 禁止EditText輸入空格
* @param editText
*/
public static void setEditTextInhibitInputSpace(EditText editText){
InputFilter filter=new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if(source.equals(" ")return "";
else return null;
}
};
editText.setFilters(new InputFilter[]{filter});
}
/**
* 禁止EditText輸入特殊字符
* @param editText
*/
public static void setEditTextInhibitInputSpeChat(EditText editText){
InputFilter filter=new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“'。,、?]";
Pattern pattern = Pattern.compile(speChat);
Matcher matcher = pattern.matcher(source.toString());
if(matcher.find())return "";
else return null;
}
};
editText.setFilters(new InputFilter[]{filter});
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
android studio logcat 無篩選 顯示全部日志 無應(yīng)用包名區(qū)分方式
這篇文章主要介紹了android studio logcat 無篩選 顯示全部日志 無應(yīng)用包名區(qū)分方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Flutter輸入框TextField屬性及監(jiān)聽事件介紹
這篇文章主要介紹了Flutter輸入框TextField屬性及監(jiān)聽事件介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-11-11
Android在多種設(shè)計(jì)下實(shí)現(xiàn)懶加載機(jī)制的方法
這篇文章主要介紹了Android在多種設(shè)計(jì)下實(shí)現(xiàn)懶加載機(jī)制的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
MVVMLight項(xiàng)目Model?View結(jié)構(gòu)及全局視圖模型注入器
這篇文章主要為大家介紹了MVVMLight項(xiàng)目中Model及View的結(jié)構(gòu)及全局視圖模型注入器的使用說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01
Flutter實(shí)現(xiàn)PopupMenu彈出式菜單按鈕詳解
這篇文章主要介紹了Flutter實(shí)現(xiàn)PopupMenu彈出式菜單按鈕,PopupMenuButton是一個(gè)用于創(chuàng)建彈出菜單的小部件,當(dāng)用戶點(diǎn)擊觸發(fā)按鈕時(shí),PopupMenuButton會(huì)在屏幕上方或下方彈出一個(gè)菜單,感興趣想要詳細(xì)了解可以參考下文2023-05-05

