Android monitor EditText text input EditText monitor events and input events

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: