Skip to content Skip to sidebar Skip to footer

Android: Two Edittexts Dependent On Each Other

I have two EditTexts. One is for example temperature in °C and the other is temperature in °F. When user edits one EditTexts I want the other to change accordingly. Problem is th

Solution 1:

Check if each EditText has focus first and only change it programmatically if it doesn't have focus.

http://developer.android.com/reference/android/view/View.html#hasFocus()

The one being edited by the user will have focus. The other one won't.

Solution 2:

Try

EditText et=newEditText(this);
final EditText et2=newEditText(this);
et1.addTextChangedListener(newTextWatcher() {

    @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
        //DO CHANGE YOUR FAHRENHEIT HERE
et2.setText(....);
    }

    @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @OverridepublicvoidafterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

EDIT: OnFocusChange of both EditText disable focus of the other.

Post a Comment for "Android: Two Edittexts Dependent On Each Other"