Android: Two EditTexts dependent on each other

谁说胖子不能爱 提交于 2019-12-13 01:25:44

问题


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 that this changes make loop, where EditTexts make changes of each other. How to solve this? I would like to still have reaction in other EditText when I change the first one even programmatically, but without loop...


回答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.




回答2:


Try

EditText et=new EditText(this);
final EditText et2=new EditText(this);
et1.addTextChangedListener(new TextWatcher() {

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

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
});

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



来源:https://stackoverflow.com/questions/17717210/android-two-edittexts-dependent-on-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!