0

我有两个 Edittext,当第一个 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) {



     }
    }
};
4

1 回答 1

1

创建 2 个文本观察器,例如 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);
                }
       }); 

    }
于 2013-06-22T22:11:11.797 回答