Android: How to enable my button back if EditText is not empty?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 06:05:16

问题


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.



来源:https://stackoverflow.com/questions/2979441/android-how-to-enable-my-button-back-if-edittext-is-not-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!