两个Edittexts的一个Textwatcher - android

时间:2013-06-22 18:50:50

标签: android android-edittext textwatcher

我有两个Edittexts,当第一个Edittext更改时,设置第二个Edittext的文本输出。当第二个Edittext更改时,设置第一个Edittext的文本输出。我该如何设置它们?

感谢

 inputValue = (EditText) findViewById(R.id.EditTextValue);
 ResultView = (EditText) findViewById(R.id.TextViewResult);
 inputValue.addTextChangedListener(textWatcher);                    
 ResultView.addTextChangedListener(textWatcher);

 public TextWatcher textWatcher = new TextWatcher() {

    private View view;

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

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

    }

    public void afterTextChanged(Editable arg0) {



     }
    }
};

1 个答案:

答案 0 :(得分:1)

创建2个textwatchers,例如am和am2

TextWatcher am,am2;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     setContentView(R.layout.YOUR_XML);


    am = new TextWatcher(){
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {
           }
           public void onTextChanged(CharSequence s, int start, int before, int count) {
           }
           public void afterTextChanged(Editable s) {

              inputValue.addTextChangedListener(this);

               ResultView.setText("");
           }
    };

    am2 = new TextWatcher(){
           public void beforeTextChanged(CharSequence s, int start, int count, int after) {       
           }
           public void onTextChanged(CharSequence s, int start, int before, int count) {
           }
           public void afterTextChanged(Editable s) {

                  ResultView.addTextChangedListener(this);

                   inputValue.setText("");
           }
     };

    inputValue.setOnFocusChangeListener(new OnFocusChangeListener(){
                public void onFocusChange (View v, boolean hasFocus){
                inputValue.removeTextChangedListener(am);
                ResultView.removeTextChangedListener(am2);
                setOnTextChangedListener(hasFocus);
                }
       }); 

    }