Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾
??監(jiān)聽事件
setOnEditorActionListener:軟鍵盤回車監(jiān)聽事件
testEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Log.e("TAG", "onEditorAction: 點擊了回車按鈕");
return false;
}
});
Kotlin代碼
testEditText.setOnEditorActionListener(OnEditorActionListener { v, actionId, event ->
Log.e("TAG", "onEditorAction: 點擊了回車按鈕")
false
})
addTextChangedListener:文本變化監(jiān)聽事件,里面有三個回調函數
beforeTextChanged(CharSequence s, int start, int count, int after)
參數一代表輸入的字符,參數二代表當前光標所在EditText整個字符串的位置,參數三一般為0,參數四代表一次性輸入了幾個字符,主要是中文狀態(tài)或直接粘貼上去的字符(數字或符號或英文都是點擊一個就顯示上去了,所以該值為1,中文一般都是打幾個字顯示上去)
onTextChanged(CharSequence s, int start, int before, int count)
基本同上面的說明
afterTextChanged(Editable s)
參數為修改后的字符
testEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//參數1代表輸入的
Log.e("TAG", "beforeTextChanged: 輸入前(內容變化前)的監(jiān)聽回調"+s.toString()+"==="+start+"==="+count+"==="+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TAG", "beforeTextChanged: 輸入中(內容變化中)的監(jiān)聽回調"+s.toString()+"==="+start+"==="+before+"==="+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.e("TAG", "beforeTextChanged: 輸入后(內容變化后)的監(jiān)聽回調"+s.toString());
}
});
Kotlin代碼
testEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
//參數1代表輸入的
Log.e("TAG", "beforeTextChanged: 輸入前(內容變化前)的監(jiān)聽回調$s===$start===$count===$after")
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
Log.e("TAG", "beforeTextChanged: 輸入中(內容變化中)的監(jiān)聽回調$s===$start===$before===$count")
}
override fun afterTextChanged(s: Editable) {
Log.e("TAG", "beforeTextChanged: 輸入后(內容變化后)的監(jiān)聽回調$s")
}
})
setOnFocusChangeListener:是否獲取焦點的監(jiān)聽
testEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.e("TAG", "onFocusChange: 是否獲取焦點:hasFocus:為true表示獲取焦點,為false表示未獲取");
}
});
Kotlin代碼
testEditText.setOnFocusChangeListener(OnFocusChangeListener { v, hasFocus ->
Log.e("TAG", "onFocusChange: 是否獲取焦點:hasFocus:為true表示獲取焦點,為false表示未獲取")
})
??InputFilter
字符過濾在項目中也是經常會遇到的業(yè)務功能(比如限制輸入小數點后兩位,比如僅限制中文輸入,比如不能輸入特殊字符,再比如WOCAO等敏感詞屏蔽)。
有的同學要說了,【android:inputType】不就是做這個的嗎,確實,但是為了兼容大多數人,必須要有取舍,因此也就有了局限性。
系統(tǒng)內置了兩個過濾:new InputFilter.AllCaps()和new InputFilter.LengthFilter(int max)
AllCaps為全部自動轉換為大寫,LengthFilter為限制字符長度最大為幾。
我們【Ctrl+左鍵】快捷鍵點進去看遠嗎,他們是繼承的【InputFilter】,所以我們也能繼承繼而實現自己的過濾規(guī)則。
InputFilter custemInputFiter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//source 新輸入的字符串
//start 新輸入的字符串起始下標,一般為0
//end 新輸入的字符串終點下標,一般為source長度-1
//dest 輸入之前文本框內容
//dstart 原內容起始坐標,一般為0
//dend 原內容終點坐標,一般為dest長度-1
if (source.toString().equals("芝麻粒兒")) {
//此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁面上表現為不顯示
return null;
}
Log.e("TAG", "filter: 自定義的過濾規(guī)則");
return null;
}
};
//傳遞的參數是數組,也就是可以有多個過濾規(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 新輸入的字符串起始下標,一般為0
//end 新輸入的字符串終點下標,一般為source長度-1
//dest 輸入之前文本框內容
//dstart 原內容起始坐標,一般為0
//dend 原內容終點坐標,一般為dest長度-1
if (source.toString() == "芝麻粒兒") {
//此示例:輸入的如果是【芝麻粒兒】,則直接返回null,頁面上表現為不顯示
return@InputFilter null
}
Log.e("TAG", "filter: 自定義的過濾規(guī)則")
null
}
//傳遞的參數是數組,也就是可以有多個過濾規(guī)則
testEditText.setFilters(
arrayOf(
custemInputFiter,
LengthFilter(6),
AllCaps()
)
)以上就是Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾的詳細內容,更多關于Android EditText監(jiān)聽InputFilter的資料請關注腳本之家其它相關文章!
相關文章
Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解
這篇文章主要介紹了Android開發(fā)之機頂盒上gridview和ScrollView的使用詳解的相關資料,需要的朋友可以參考下2016-02-02

