Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the View in TextWatcher method context?

I have a handler that is a TextWatcher and i dont know how to get the View that has changed text.

Here is my handler:

TextWatcher handler = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        oldText = s.toString();
    }

    @Override
    public void afterTextChanged(Editable s) {
        //v.setText("afterTextChanged");
    }
};

Note the commented part, that is what i want, to get the View from the EditText that has triggered the event, to change the text after the text was changed.

How i can reach this .setText() method inside the afterTextChanged event? (Like onClick event that view is v)

like image 882
Paulo Roberto Rosa Avatar asked Apr 04 '14 14:04

Paulo Roberto Rosa


People also ask

How do I get view on TextWatcher?

TextWatcher handler = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { oldText = s.

What is Text watcher?

Android EditText with TextWatcher (Searching data from ListView) Android EditText is a subclass of TextView. EditText is used for entering and modifying text. While using EditText width, we must specify its input type in inputType property of EditText which configures the keyboard according to input.

How do you use kotlin text watcher?

Android Dependency Injection using Dagger with Kotlin This example demonstrates how to use the TextWatcher class in kotlin. Step 1 − Create a new project in Android Studio, go to File ⇒New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

What is TextWatcher in Android?

TextWatcher is a useful class provided by the Android Developer API. It can be used to watch a input text field and you can instantly update data on other views. It can be useful for counting the number of characters entered in the text field instantly and measuring password strength on entering etc.


1 Answers

public static class MyTextWatcher implements TextWatcher {

    private EditText mEditText;

    public MyTextWatcher(EditText editText) {
        mEditText = editText;
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        oldText = mEditText.toString();
    }
    ....
}

Add it with:

    mFirstEditText.addTextChangedListener(new MyTextWatcher(mFirstEditText));
like image 71
Ion Aalbers Avatar answered Oct 23 '22 11:10

Ion Aalbers