Reputation: 1967
this is really strange, yesterday I asked the question Yesterdays question how to implement multiple editText listeners to avoid duplicating my code. I was kindly provided an answer (which I swear I tried out) but today I am getting no where with it. I am trying to do the following but am getting an Cannot instantiate the type TextWatcher
error when trying to setup the tw Textwatcher.
**TextWatcher tw = new TextWatcher();**
intTextValue.addTextChangedListener(tw);
any help would be much appreciated. its starting to drive me a little crazy.
Ultimately I am trying to get to the following situation (which should be simple 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
}
}
Upvotes: 4
Views: 9157
Reputation: 303
Late to the game, but here's how I do it in a programmatic way if you don't know ahead of time how many listeners you'll need. Just have a function return a new listener every time you create a new view and then attach it to that 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) {
}
};
}
Upvotes: 0
Reputation: 389
Also make sure that ids are correct R.id.et is used for all the three EditText s
Upvotes: 0
Reputation: 11422
TextWatcher is an interface. You cannot instanciate interfaces. A class needs to implement the interface and this class can be instanciated. So you need to create a class, doesnt matter if anonymous class or not, and instanciate 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
}
};
Upvotes: 0
Reputation: 1632
You don't need a TextWatcher instance, just use "this"
et.addTextChangedListener(this);
et1.addTextChangedListener(this);
et2.addTextChangedListener(this);
Upvotes: 4