Implementing multiple TextWatchers on the same Activity
I implement TextWatcher in the Activity:
public class Coordinate extends Activity implements TextWatcher {
/** Called when the activity is firs开发者_高级运维t created. */
......
Then
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
........
Part of my problem is that having more than one TextChangedListener causes the app to FC
txtDdLatDeg.addTextChangedListener(this);
txtDMmLatDeg.addTextChangedListener(this);
txtDMSLatDeg.addTextChangedListener(this);
Then
@Override
public void afterTextChanged(Editable s) {
String c = s.toString(); // read Content
// stuff to do later
((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c);
((EditText)findViewById(R.id.txtDdLatDeg)).setText(c);
return;
} // End of TextChanged method
I need to be able to update one EditText and have the other two update on the fly.
I can only seem to make it works when only one EditText has theaddChangeListener
.
I also cannot seem to implement a seperate afterTextChanged
method for the individual EditText fields.Then create them as instance variables:
TextWatcher watcher1 = 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) {}
@Override
public void afterTextChanged(Editable s) { }
};
TextWatcher watcher2 = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence c, int i, int i1, int i2) {}
@Override
public void onTextChanged(CharSequence c, int i, int i1, int i2) {}
@Override
public void afterTextChanged(Editable s) { }
};
Then you can do:
txtDdLatDeg.addTextChangedListener(watcher1);
txtDMmLatDeg.addTextChangedListener(watcher1);
Ok, I solved this by using onFocus()
on the EditText before the afterTextChanged
method:
onCreate (Bundle icicle) {
// Usual stuff here
txtDdLatDeg.addTextChangeListener(watcher1);
}
TextWatcher watcher1 = new TextWatcher() {
if (txtDdLatDeg.hasFocus()) {
@Override
public void afterTextChanged(Editable s) {
String c = s.toString();
((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c);
((EditText)findViewById(R.id.txtDMmLatDeg)).setText(c);
}
}};
I create an instance variable for each EditText box I need to watch/manipulate.
精彩评论