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)文章
Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Flutter實(shí)現(xiàn)底部和頂部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
Android仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼
這篇文章主要介紹了Android之仿今日頭條頂部導(dǎo)航欄效果的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法
這篇文章主要介紹了Android使用TextView實(shí)現(xiàn)無(wú)下劃線超鏈接的方法,結(jié)合實(shí)例形式分析了Android中TextView超鏈接去除下劃線的相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2016-08-08
android實(shí)現(xiàn)banner輪播圖無(wú)限輪播效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)banner輪播圖無(wú)限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
android開(kāi)機(jī)自啟動(dòng)APP及使用adb命令測(cè)試方法
今天小編就為大家分享一篇android開(kāi)機(jī)自啟動(dòng)APP及使用adb命令測(cè)試方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android開(kāi)發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開(kāi)發(fā)之機(jī)頂盒上gridview和ScrollView的使用詳解的相關(guān)資料,需要的朋友可以參考下2016-02-02
利用Kotlin開(kāi)發(fā)你的第一個(gè)Android應(yīng)用
Kotlin最近真的是大熱啊,所以下面這篇文章主要給大家介紹了關(guān)于利用Kotlin開(kāi)發(fā)你的第一個(gè)Android應(yīng)用的相關(guān)資料,文中將實(shí)現(xiàn)的步驟介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-10-10

