Android: How to enable my button back if EditText

2019-02-18 12:43发布

问题:

I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.

Can anyone help me with this?

summit.setEnabled(false);

buttonEnable();

public void buttonEnable(){
    if (feedback.length()>0 && email.length()>0){
        summit.setEnabled(true);
    }else{
        summit.setEnabled(false);
    }
}

回答1:

You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)



回答2:

One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.