Question

I implement TextWatcher in the Activity:

public class Coordinate extends Activity implements TextWatcher {
/** Called when the activity is first 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 the addChangeListener.
I also cannot seem to implement a seperate afterTextChanged method for the individual EditText fields.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top