😜Steer events
setOnEditorActionListener: Soft keyboard input listening event
(new () { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { ("TAG", "onEditorAction: Clicked on Enter button"); return false; } });
Kotlin code
(OnEditorActionListener { v, actionId, event -> ("TAG", "onEditorAction: Clicked on Enter button") false })
addTextChangedListener: Text change listening event, there are three callback functions
beforeTextChanged(CharSequence s, int start, int count, int after)
Parameter 1 represents the input character, Parameter 2 represents the position of the entire string of EditText where the current cursor is located, Parameter 3 is generally 0, Parameter 4 represents several characters entered at one time, mainly Chinese status or characters directly pasted (numbers or symbols or English are displayed when clicking on one, so the value is 1, and Chinese is usually displayed with a few words)
onTextChanged(CharSequence s, int start, int before, int count)
Basically the same as the above description
afterTextChanged(Editable s)
The parameters are modified characters
(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // Parameter 1 represents the input ("TAG", "beforeTextChanged: Listening callback before input (before content changes)"+()+"==="+start+"==="+count+"==="+after); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { ("TAG", "beforeTextChanged: Listening callback in input (content changes)"+()+"==="+start+"==="+before+"==="+count); } @Override public void afterTextChanged(Editable s) { ("TAG", "beforeTextChanged: Listening callback after input (after content changes)"+()); } });
Kotlin code
(object : TextWatcher { override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { // Parameter 1 represents the input ("TAG", "beforeTextChanged: Before entering(Before the content changes)Listening callback$s===$start===$count===$after") } override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { ("TAG", "beforeTextChanged: Input(Content is changing)Listening callback$s===$start===$before===$count") } override fun afterTextChanged(s: Editable) { ("TAG", "beforeTextChanged: Listening callback $s after input (after content changes)") } })
setOnFocusChangeListener: Whether to get the focus listening
(new () { @Override public void onFocusChange(View v, boolean hasFocus) { ("TAG", "onFocusChange: Whether to get focus: hasFocus: true means to get focus, false means not get"); } });
Kotlin code
(OnFocusChangeListener { v, hasFocus -> ("TAG", "onFocusChange: Whether to get focus: hasFocus: true means to get focus, false means not get") })
😜InputFilter
Character filtering is also a business function that is often encountered in projects (such as limiting input of two decimal places, such as limiting input in Chinese, such as not being able to enter special characters, and other sensitive words such as WOCAO).
Some students want to say, isn't [android:inputType] just doing this? It's true, but in order to be compatible with most people, there must be trade-offs, so there are limitations.
The system has two built-in filters: new () and new (int max)
AllCaps is automatically converted to uppercase, and LengthFilter is the maximum limit of character length.
Do we click the [Ctrl+left key] shortcut key to see far? They inherit [InputFilter], so we can also inherit and implement our own filtering rules.
InputFilter custemInputFiter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { //source New input string //start The starting subscript of the newly entered string is generally 0 //end The new input string end point subscript is generally source length -1 //dest Enter the content of the text box before entering //dstart The original content start coordinate, generally 0 //dend The original content end coordinate, generally dest length -1 if (().equals("Sesame seeds")) { //This example: If the input is [Sesame Lime], it will be returned directly to null, and the page will not be displayed return null; } ("TAG", "filter: Custom filtering rules"); return null; } }; //The passed parameter is an array, that is, there can be multiple filtering rules(new InputFilter[]{ custemInputFiter, new (6), new ()});
Kotlin code
val custemInputFiter = InputFilter { source, start, end, dest, dstart, dend -> //source New input string //start The starting subscript of the newly entered string is generally 0 //end The new input string end point subscript is generally source length -1 //dest Enter the content of the text box before entering //dstart The original content start coordinate, generally 0 //dend The original content end coordinate, generally dest length -1 if (() == "Sesame seeds") { //This example: If the input is [Sesame Lime], it will be returned directly to null, and the page will not be displayed return@InputFilter null } ("TAG", "filter: Custom filtering rules") null } //The passed parameter is an array, that is, there can be multiple filtering rules( arrayOf( custemInputFiter, LengthFilter(6), AllCaps() ) )
The above is the detailed content of Android development EditText prohibits input monitoring and InputFilter character filtering. For more information about Android EditText monitoring InputFilter, please follow my other related articles!