How to get the View in TextWatcher method context?

蓝咒 提交于 2019-12-19 05:12:42

问题


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)


回答1:


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));


来源:https://stackoverflow.com/questions/22866060/how-to-get-the-view-in-textwatcher-method-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!