Skip to content Skip to sidebar Skip to footer

KeyReleased Equivalence In Android

On PC I can add a onKeyListener for a JTextField to listen keyReleased event. On Android I've used addTextChangedListener. I have two EditText fields in my Android application. Edi

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(new OnFocusChangeListener() {

                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        ((EditText) v).addTextChangedListener(new TextWatcher() {

                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                             //

                        }

                        public void beforeTextChanged(CharSequence s, int start, int count,
                                int after) {
                            // 

                        }

                        public void afterTextChanged(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 EditTexts. Then, when you receive a text change event, before updating other text edits, just unregister old listeners, update text and enable listeners again:

class SynchronizingWatcher implements TextWatcher {
  Set<EditText> synchronizedViews = new HashSet<EditText>();

  public void watchView(EditText view) {
    view.addTextChangedListener(this);
    synchronizedViews.add(view);
  }

  public void afterTextChanged(Editable s) {
    for (EditText editText : synchronizedViews) {
      editText.removeTextChangeListener(this);
      editText.setText(s);  // Of course you can do something more complicated here.
      editText.addTextChangeListener(this);
    }
  }

  public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // Don't care.
  }

  public void onTextChanged(CharSequence s, int start, int before, int count) {
    // Don't care.
  }
}

...
// Somewhere in your activity:
SyncrhonizingWatcher synchronizingWatcher = new SynchronizingWatcher();
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"