Keyreleased Equivalence In Android
Solution 1:
Attach a onFocusChangedListener and add the TextChangedListener when a EditText has focus and remove it when it loses focus. Something like this:
EditText1.setOnFocusChangeListener(newOnFocusChangeListener() {
publicvoidonFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText) v).addTextChangedListener(newTextWatcher() {
publicvoidonTextChanged(CharSequence s, int start, int before, int count) {
//
}
publicvoidbeforeTextChanged(CharSequence s, int start, int count,
int after) {
//
}
publicvoidafterTextChanged(Editable s) {
// affect EditText2
}
});
}
if(!hasFocus){
((EditText) v).removeTextChangedListener();
}
}
});
}
});
The same for EditText2
Solution 2:
First of all, I would create one text change listener, something like SynchronizingWatcher
and attach it to both EditText
s. Then, when you receive a text change event, before updating other text edits, just unregister old listeners, update text and enable listeners again:
classSynchronizingWatcherimplementsTextWatcher {
Set<EditText> synchronizedViews = newHashSet<EditText>();
publicvoidwatchView(EditText view) {
view.addTextChangedListener(this);
synchronizedViews.add(view);
}
publicvoidafterTextChanged(Editable s) {
for (EditText editText : synchronizedViews) {
editText.removeTextChangeListener(this);
editText.setText(s); // Of course you can do something more complicated here.
editText.addTextChangeListener(this);
}
}
publicvoidbeforeTextChanged(CharSequence s, int start, int count, int after) {
// Don't care.
}
publicvoidonTextChanged(CharSequence s, int start, int before, int count) {
// Don't care.
}
}
...
// Somewhere in your activity:SyncrhonizingWatchersynchronizingWatcher=newSynchronizingWatcher();
synchronizingWatcher.watchView(myEditText1);
synchronizingWatcher.watchView(myEditText1);
Another solution: provide your own KeyListener
that decorates existing KeyListener
(you can get existing key listener with editText.getKeyListener()
and set your decorator with editText.setKeyListener()
. Your decorator would also update other edit texts in onKeyUp()
. But I would try to stay away from messing with that stuff.
Post a Comment for "Keyreleased Equivalence In Android"