-4

我在布局中有两个编辑文本字段。当我在 edittext1 中键入内容时,相同的文本应该在 edittext2 中动态填充,反之亦然。我怎样才能做到这一点??

4

2 回答 2

3

您将TextWatcher添加到 Edittext 如下

    EditText myTextBox = (EditText) findViewById(R.id.myTextBox);
EditText myOutputBox = (EditText) findViewById(R.id.myOutputBox);
      myTextBox.addTextChangedListener(new TextWatcher() {

       public void afterTextChanged(Editable s) {
       }

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

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) {
       myOutputBox.setText(s);
       }
      });

其他 Edittext 也是如此。

 myOutputBox.addTextChangedListener(new TextWatcher() {

           public void afterTextChanged(Editable s) {
           }

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

           public void onTextChanged(CharSequence s, int start, 
             int before, int count) {
           myTextBox.setText(s);
           }
          });
于 2013-10-14T07:39:58.803 回答
1

Take a look at EditText#addTextChangedListener. All you need to do is update the desired EditText inside afterTextChanged method.

于 2013-10-14T07:34:35.083 回答