網站首頁 編程語言 正文
??監聽事件
setOnEditorActionListener:軟鍵盤回車監聽事件?
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:文本變化監聽事件,里面有三個回調函數
beforeTextChanged(CharSequence s, int start, int count, int after)
參數一代表輸入的字符,參數二代表當前光標所在EditText整個字符串的位置,參數三一般為0,參數四代表一次性輸入了幾個字符,主要是中文狀態或直接粘貼上去的字符(數字或符號或英文都是點擊一個就顯示上去了,所以該值為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: 輸入前(內容變化前)的監聽回調"+s.toString()+"==="+start+"==="+count+"==="+after);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.e("TAG", "beforeTextChanged: 輸入中(內容變化中)的監聽回調"+s.toString()+"==="+start+"==="+before+"==="+count);
}
@Override
public void afterTextChanged(Editable s) {
Log.e("TAG", "beforeTextChanged: 輸入后(內容變化后)的監聽回調"+s.toString());
}
});
Kotlin代碼
testEditText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
//參數1代表輸入的
Log.e("TAG", "beforeTextChanged: 輸入前(內容變化前)的監聽回調$s===$start===$count===$after")
}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
Log.e("TAG", "beforeTextChanged: 輸入中(內容變化中)的監聽回調$s===$start===$before===$count")
}
override fun afterTextChanged(s: Editable) {
Log.e("TAG", "beforeTextChanged: 輸入后(內容變化后)的監聽回調$s")
}
})
setOnFocusChangeListener:是否獲取焦點的監聽
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
字符過濾在項目中也是經常會遇到的業務功能(比如限制輸入小數點后兩位,比如僅限制中文輸入,比如不能輸入特殊字符,再比如WOCAO等敏感詞屏蔽)。
有的同學要說了,【android:inputType】不就是做這個的嗎,確實,但是為了兼容大多數人,必須要有取舍,因此也就有了局限性。
系統內置了兩個過濾:new InputFilter.AllCaps()和new InputFilter.LengthFilter(int max)
AllCaps為全部自動轉換為大寫,LengthFilter為限制字符長度最大為幾。
我們【Ctrl+左鍵】快捷鍵點進去看遠嗎,他們是繼承的【InputFilter】,所以我們也能繼承繼而實現自己的過濾規則。
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: 自定義的過濾規則");
return null;
}
};
//傳遞的參數是數組,也就是可以有多個過濾規則
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: 自定義的過濾規則")
null
}
//傳遞的參數是數組,也就是可以有多個過濾規則
testEditText.setFilters(
arrayOf(
custemInputFiter,
LengthFilter(6),
AllCaps()
)
)
原文鏈接:https://juejin.cn/post/7058939025637244964
相關推薦
- 2023-05-06 Python執行ping操作的簡單方法_python
- 2022-06-12 C#集合之有序列表的用法_C#教程
- 2023-02-23 Android開發之BottomSheetDialog組件的使用_Android
- 2022-06-29 python人工智能tensorflow函數tf.get_variable使用方法_python
- 2022-05-06 Python學習之循環方法詳解_python
- 2022-07-02 matplotlib之pyplot模塊添加文本、注解(text和annotate)_python
- 2022-11-20 關于rust的模塊引入問題_相關技巧
- 2022-06-13 Docker執行DockerFile構建過程指令解析_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支