简体   繁体   中英

Android - check an EditText input box each type the user types in a character

I am trying to check each time a user types a character into a EditText box.

Is there some type of OnChangeListener ?

You can use the textChangeListener to detect if the edittext values is changed upon clicking from the input keyboard

sample:

edditext.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {}
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){
            //s is the current character in the eddittext after it is changed
    }
}); 

@Rod_Algonquin's answer (accepted) mentioned that the s parameter is all the characters in the EditText. What I was looking for was a way to get the specific text added, so I just adjusted what was mentioned above.

private void setTextChangedListener(EditText editText) {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String inputString = String.valueOf(s.subSequence(start, start + count));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

inputString is the actual text added to the EditText.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM