Android multiple EditText with OnTextChangedListener

This is really strange, yesterday I asked the Yesterdays question how to implement multiple editText listeners to avoid duplication of my code. I was kindly provided with the answer (to which I swear I tried), but today I do not get any relation with him. I try to do the following, but when I try to tweak tw Textwatcher I get Cannot instantiate the type TextWatcher .

 **TextWatcher tw = new TextWatcher();** intTextValue.addTextChangedListener(tw); 

Any help would be greatly appreciated. its beginning confused me a little.

I end up trying to move on to the next situation (what should be just right?).

 public class myClass extends Activity implements OnFocusChangeListener, TextWatcher { private EditText et; private EditText et1; private EditText et2; private int whoHasFocus= 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout1); et = (EditText)findViewById(R.id.et); et.setOnFocusChangeListener(this); et.addTextChangedListener(tw); et1 = (EditText)findViewById(R.id.et); et1.setOnFocusChangeListener(this); et1.addTextChangedListener(tw); et2 = (EditText)findViewById(R.id.et); et2.setOnFocusChangeListener(this); et2.addTextChangedListener(tw); } @Override public void onFocusChange(View v, boolean hasFocus) { switch (v.getId()) { case R.id.et: whoHasFocus =1; break; case R.id.et1: whoHasFocus =2; break; case R.id.et2: whoHasFocus=3; break; } } @Override public void afterTextChanged(Editable s) { switch (whoHasFocus) { case 1: //do code break; case 2: //do code break; case 3: //do code break; } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } } 
+4
source share
4 answers

You do not need an instance of TextWatcher, just use "this"

 et.addTextChangedListener(this); et1.addTextChangedListener(this); et2.addTextChangedListener(this); 
+4
source

TextWatcher is an interface. You cannot control interfaces. A class must implement an interface, and this class can be instantiated. So you need to create a class, it doesn't matter if the class is anonymous or not, and initiate this.

Example:

 TextWatcher tw = 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) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }; 
0
source

Also make sure the identifiers are correct R.id.et is used for all three EditText s

0
source

It's late for the game, but here's how I do it programmatically, if you don't know in advance how many listeners you will need. The function simply returns a new listener each time you create a new view, and then attach it to this view.

 private TextWatcher getTextWatcher() { return 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) { } @Override public void afterTextChanged(Editable s) { } }; } 
0
source

Source: https://habr.com/ru/post/1402475/


All Articles