Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾
??監(jiān)聽(tīng)事件
setOnEditorActionListener:軟鍵盤(pán)回車(chē)監(jiān)聽(tīng)事件
testEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.e("TAG", "onEditorAction: 點(diǎn)擊了回車(chē)按鈕");
return false;
}
});
Kotlin代碼
testEditText.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
Log.e("TAG", "onEditorAction: 點(diǎn)擊了回車(chē)按鈕")
false
})
addTextChangedListener:文本變化監(jiān)聽(tīng)事件,里面有三個(gè)回調(diào)函數(shù)
beforeTextChanged(CharSequence s, int start, int count, int after)
參數(shù)一代表輸入的字符,參數(shù)二代表當(dāng)前光標(biāo)所在EditText整個(gè)字符串的位置,參數(shù)三一般為0,參數(shù)四代表一次性輸入了幾個(gè)字符,主要是中文狀態(tài)或直接粘貼上去的字符(數(shù)字或符號(hào)或英文都是點(diǎn)擊一個(gè)就顯示上去了,所以該值為1,中文一般都是打幾個(gè)字顯示上去)
onTextChanged(CharSequence s, int start, int before, int count)
基本同上面的說(shuō)明
afterTextChanged(Editable s)
參數(shù)為修改后的字符
testEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//參數(shù)1代表輸入的
Log.e("TAG", "beforeTextChanged: 輸入前(內(nèi)容變化前)的監(jiān)聽(tīng)回調(diào)"+s.toString()+"==="+start+"==="+count+"==="+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TAG", "beforeTextChanged: 輸入中(內(nèi)容變化中)的監(jiān)聽(tīng)回調(diào)"+s.toString()+"==="+start+"==="+before+"==="+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.e("TAG", "beforeTextChanged: 輸入后(內(nèi)容變化后)的監(jiān)聽(tīng)回調(diào)"+s.toString());
}
});
Kotlin代碼
testEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
//參數(shù)1代表輸入的
Log.e("TAG", "beforeTextChanged: 輸入前(內(nèi)容變化前)的監(jiān)聽(tīng)回調(diào)$s===$start===$count===$after")
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
Log.e("TAG", "beforeTextChanged: 輸入中(內(nèi)容變化中)的監(jiān)聽(tīng)回調(diào)$s===$start===$before===$count")
}
override fun afterTextChanged(s: Editable) {
Log.e("TAG", "beforeTextChanged: 輸入后(內(nèi)容變化后)的監(jiān)聽(tīng)回調(diào)$s")
}
})
setOnFocusChangeListener:是否獲取焦點(diǎn)的監(jiān)聽(tīng)
testEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.e("TAG", "onFocusChange: 是否獲取焦點(diǎn):hasFocus:為true表示獲取焦點(diǎn),為false表示未獲取");
}
});
Kotlin代碼
testEditText.setOnFocusChangeListener(OnFocusChangeListener { v, hasFocus ->
Log.e("TAG", "onFocusChange: 是否獲取焦點(diǎn):hasFocus:為true表示獲取焦點(diǎn),為false表示未獲取")
})
??InputFilter
字符過(guò)濾在項(xiàng)目中也是經(jīng)常會(huì)遇到的業(yè)務(wù)功能(比如限制輸入小數(shù)點(diǎn)后兩位,比如僅限制中文輸入,比如不能輸入特殊字符,再比如WOCAO等敏感詞屏蔽)。
有的同學(xué)要說(shuō)了,【android:inputType】不就是做這個(gè)的嗎,確實(shí),但是為了兼容大多數(shù)人,必須要有取舍,因此也就有了局限性。
系統(tǒng)內(nèi)置了兩個(gè)過(guò)濾:new InputFilter.AllCaps()和new InputFilter.LengthFilter(int max)
AllCaps為全部自動(dòng)轉(zhuǎn)換為大寫(xiě),LengthFilter為限制字符長(zhǎng)度最大為幾。
我們【Ctrl+左鍵】快捷鍵點(diǎn)進(jìn)去看遠(yuǎn)嗎,他們是繼承的【InputFilter】,所以我們也能繼承繼而實(shí)現(xiàn)自己的過(guò)濾規(guī)則。
InputFilter custemInputFiter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//source 新輸入的字符串
//start 新輸入的字符串起始下標(biāo),一般為0
//end 新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
//dest 輸入之前文本框內(nèi)容
//dstart 原內(nèi)容起始坐標(biāo),一般為0
//dend 原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
if (source.toString().equals("芝麻粒兒")) {
//此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁(yè)面上表現(xiàn)為不顯示
return null;
}
Log.e("TAG", "filter: 自定義的過(guò)濾規(guī)則");
return null;
}
};
//傳遞的參數(shù)是數(shù)組,也就是可以有多個(gè)過(guò)濾規(guī)則
testEditText.setFilters(new InputFilter[]{
custemInputFiter,
new InputFilter.LengthFilter(6),
new InputFilter.AllCaps()});
Kotlin代碼
val custemInputFiter = InputFilter { source, start, end, dest, dstart, dend -> //source 新輸入的字符串
//start 新輸入的字符串起始下標(biāo),一般為0
//end 新輸入的字符串終點(diǎn)下標(biāo),一般為source長(zhǎng)度-1
//dest 輸入之前文本框內(nèi)容
//dstart 原內(nèi)容起始坐標(biāo),一般為0
//dend 原內(nèi)容終點(diǎn)坐標(biāo),一般為dest長(zhǎng)度-1
if (source.toString() == "芝麻粒兒") {
//此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁(yè)面上表現(xiàn)為不顯示
return@InputFilter null
}
Log.e("TAG", "filter: 自定義的過(guò)濾規(guī)則")
null
}
//傳遞的參數(shù)是數(shù)組,也就是可以有多個(gè)過(guò)濾規(guī)則
testEditText.setFilters(
arrayOf(
custemInputFiter,
LengthFilter(6),
AllCaps()
)
)以上就是Android開(kāi)發(fā)EditText禁止輸入監(jiān)聽(tīng)及InputFilter字符過(guò)濾的詳細(xì)內(nèi)容,更多關(guān)于Android EditText監(jiān)聽(tīng)I(yíng)nputFilter的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法
OkHttp包為安卓開(kāi)發(fā)中的HTTP協(xié)議網(wǎng)絡(luò)編程帶來(lái)了很大的便利,這里我們就來(lái)看一下最基本的、Android中使用OkHttp包處理HTTP的get和post請(qǐng)求的方法:2016-07-07
Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例
本篇文章主要介紹了Android Recyclerview實(shí)現(xiàn)水平分頁(yè)GridView效果示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android Studio啟動(dòng)報(bào)錯(cuò)Java 1.8 or later is required的解決方法
這篇文章主要為大家詳細(xì)介紹了Android Studio啟動(dòng)時(shí)報(bào)錯(cuò)Java 1.8 or later is required的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式實(shí)例分享
這篇文章主要介紹了android 網(wǎng)絡(luò)編程之網(wǎng)絡(luò)通信幾種方式,有需要的朋友可以參考一下2013-12-12
android實(shí)現(xiàn)圓形漸變進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)圓形漸變進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android 自動(dòng)化測(cè)試經(jīng)驗(yàn)分享 深入U(xiǎn)iScrollable
UiScrollable是一個(gè)UiCollection(這東西還沒(méi)搞懂),我們可以使用它,在可滑動(dòng)的頁(yè)面(水平滑動(dòng)或上下滑動(dòng)都可以)上查找我們想要的控件(item)2013-05-05

