Yachee
Yachee

Reputation: 1

one Textwatcher for two Edittexts - android

I have two Edittexts, and when the first Edittext changes, set the text output for the second Edittext. and when the second Edittext changes, set the text output for the first Edittext. How do I set them up?

thanks

 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) {



     }
    }
};

Upvotes: 0

Views: 646

Answers (1)

Asiimwe
Asiimwe

Reputation: 2259

create 2 textwatchers e.g am and 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);
                }
       }); 

    }

Upvotes: 1

Related Questions