Method 1: use setonkeylistener (), which can only listen to hard keyboard events, so most Android devices can’t use it
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(TAG, editText.getText());
return false;
}
});
method 2: use textwatcher class to monitor the soft keyboard and hard keyboard, just implement the ontextchanged method. In addition, textwatcher also provides the beforetextchanged and aftertextchanged methods for more detailed input monitoring
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.i(TAG, "please input the status in the word, count is the number of the string your input");
Log.i(TAG, editText.getText());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
Log.i(TAG, "input the status after your text");
}
@Override
public void afterTextChanged(Editable s) {
Log.i(TAG, "input the status before your text");
}
});
Method 3: when the input value reaches the maximum value, it is not allowed to input again. In addition, EditText does not provide us with the function of forbidding input of EditText. The following method also realizes this function.
private void setEditable(EditText editText, int maxLength, boolean value) {
if (value) {
editText.setFilters(new InputFilter[] { new MyEditFilter(maxLength) });
editText.setCursorVisible(true);
editText.setFocusableInTouchMode(true);
editText.requestFocus();
} else {
editText.setFilters(new InputFilter[] { new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start,
int end, Spanned dest, int dstart, int dend) {
return source.length() < 1 ?dest.subSequence(dstart, dend)
: "";
}
} });
editText.setCursorVisible(false);
editText.setFocusableInTouchMode(false);
editText.clearFocus();
}
}
Read More:
- Android Error: Consumer closed input channel or an error occurred. events=0x9
- Android learning — cannot resolve symbol ‘EditText‘
- Android solves the gliding problem of EditText
- Android EditText cursor control, color modification, show and hide
- Android ADB simulates click events of sliding buttons
- Vue element El input search to achieve anti shake @ input event requests frequently
- Ambari failed to monitor yarn ResourceManager, restart invalid
- How to restrict input field to only input pure numbers in HTML
- [Solved] org.mongodb.driver.cluster – Exception in monitor thread while connecting to server node1:27017
- Error installing OS X virtual machine vcu-0: verify vmcore / VM / main / physmem_ monitor.c:1123
- Velt-0.1.3 development: generating events
- Uni-app: How to Solve Native scroll events fail
- Assertion `input_val >= zero && input_val <= one` failed
- Viewing events.out.tfevents file visually in tensorboard
- JQuery is a solution to the disappearance of listening events after adding elements with append
- Events.js:167 throw er appears when starting node service under linux; // Unhandled ‘error’ event solution
- (Virtual machine monitor failed and Virtual ethernet failed)
- Error code: events.js:183 throw er; // Unhandled’error’ event—solution
- solve events.js:174 Throw er; / / error reported by unhandled ‘error’ event
- Error reported in react events.js:160 throw er; // Unhandled’error’ event