Domanda

So the problem is that when I change focus from a EditText to another I toast the selected one,but I'm instead of just one Toast of the selected EditText I get the previously selected one then I get my result of the selected one.I don't know how to use the TextWatcher Here is a sample of the code :

heure.setOnFocusChangeListener(new OnFocusChangeListener() {



            @Override
            public void onFocusChange(View arg0, boolean arg1) {
                datee.removeTextChangedListener(yourTextWatcher);
                Toast.makeText(RendezVousActivity.this,"heure", 5000).show();


            }
        });


        datee.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                heure.removeTextChangedListener(yourTextWatcher);
                Toast.makeText(RendezVousActivity.this,"Date", 5000).show();

            }
        });
È stato utile?

Soluzione

OnFocusChanged is called twice: Once for when the EditText is brought into focus, and once when it loses focus.

So when you click on an EditText, it makes a toast. Then when you click on another EditText, the first one loses focus, which shows the first toast again, and then the second one gains focus, which shows your second toast.

If you only want to show the toast when it is selected, try using:

heure.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View arg0, boolean arg1) {
        datee.removeTextChangedListener(yourTextWatcher);
        if(v.isFocused()) {
            Toast.makeText(RendezVousActivity.this,"heure", 5000).show();
        }
    }
});

datee.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        heure.removeTextChangedListener(yourTextWatcher);
        if(v.isFocused()) {
            Toast.makeText(RendezVousActivity.this,"Date", 5000).show();
        }
    }
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top