Android: How to enable my button back if EditText

2019-02-18 12:54发布

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);
    }
}

2条回答
爷、活的狠高调
2楼-- · 2019-02-18 13:17

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.

查看更多
家丑人穷心不美
3楼-- · 2019-02-18 13:18

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

查看更多
登录 后发表回答