简体   繁体   中英

What is the equivalent of OnInputListener in Android

I have an editText in android and i'ld like to listen for when an input or text has been added.

i am setting the text dynamically and i want a situation where once that is done, a new event is triggered.

 code.setText(savedcode);

code.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

i've used the text watcher but it doesn't trigger event when input is set dynamically unless i edit the field then it does. i know in javascript, on input listener can do that so i'm looking for its android equivalent. Thank you

Well, the thing is that your code seems to be correct, the one thing that is done wrong is the order.

Practically,you want to know when the textView's text has changed,but you change it before adding a text to it.

So the logical way to go is this:

TextView TV;

TV = (TextView) rootView.findViewById(R.id.theID);

TV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                Toast.makeText(Verification.this, "New Input" , Toast.LENGTH_SHORT ).show();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

TV.setText(savedcode);

This way, you change the text once you actually have a listener there, not before.That should fix your problem.

PS: I am not sure how you declare your textView, don't mind the variables name, no inspiration at this moment.

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