Ayman
Ayman

Reputation: 1009

getText vs onTextChanged charSequence

I am learning how to write applications for android and have a question. What is the difference between using a TextWatcher and within the the onTextChanged Method, setting a string value equal to the CharSequence argument and simply using getText method.

private TextWatcher passwordListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            try{
                password = charSequence.toString();
            }

            catch(Exception e){
                password=null;
            }
        }

vs

password = password_EditText.getText().toString();

Upvotes: 0

Views: 973

Answers (2)

iliaokun
iliaokun

Reputation: 29

TextWatcher can dynamic listener text changge , more Powerful text processing。 getText() is static get.

Upvotes: 0

Curtis Murphy
Curtis Murphy

Reputation: 304

You would use a text watcher if you want to be notified that the text has been changed and need to do something in direct response to that event itself. For example if you wanted to do some custom validation on the fly and automatically enable / disable a button based on the value entered.

Using getText just simply returns what ever is in the edit text field at that particular time. You might use this if you want to get the text value as a result of some other event like a button click.

Upvotes: 1

Related Questions