Doubt regarding Textwatcher - Android

Asked

Viewed 27 times

2

all right? I’m having trouble with the Textwatcher. Like, I created two "plaintext" that I named "et_texto1" and "et_texto2", when I write something in "et_texto1" it has to appear the word "Pleasure!" in "et_texto2" and when I write something in "et_texto2", the word "Hello" in "et_texto1" has to appear, but the way I put it, is giving conflict, could someone help me fix this problem? Follow my code right below!

    EditText et_texto1, et_texto2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_texto1 = (EditText) findViewById(R.id.et_texto1);
        et_texto2 = (EditText) findViewById(R.id.et_texto1);

        et_texto1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                et_texto2.setText("olá");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et_texto1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                et_texto2.setText("Prazer!");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
}```

1 answer

1

You’re adding the two TextWatcher at the same EditText. The right thing would be:

EditText et_texto1, et_texto2;
boolean ativa = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_texto1 = (EditText) findViewById(R.id.et_texto1);
    et_texto2 = (EditText) findViewById(R.id.et_texto1);
    et_texto2.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        
            if (!ativa) {
                return;
            }

            ativa = false;
            et_texto1.setText("olá");
            ativa = true;
        
        }

        @Override
        public void afterTextChanged(Editable s) {
    
        }   
    });

    et_texto1.addTextChangedListener(new TextWatcher() {

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

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (!ativa) {
                return;
            }

            ativa = false;
            et_texto2.setText("Prazer!");
            ativa = true;
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

Another thing, see that I added a new variable ativa.

When she’s marked as true, our code does not make the text change in the other EditText. The reason is very simple: You would enter one loop infinite without this variable. Look, when you type something in et_texto2, for example, it goes there and changes the et_texto1. That change in the et_texto1 triggers a new change in the et_texto2, and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.