简体   繁体   中英

Having two editText in a dialog box and set the positive button as enable based on these

I have three editTexts in my dialog view. At first, I disable positive button and every thing is ok("ok" means that when the dialog is showed, the positive button is disable and this is what I want ). Now,I want to enable it if the length of my three editTexts be larger than zero. I have found some answers only for one editText. Can anyone help me?

You could use the TextWatcher for each EditText to monitor the string length.

    boolean et1HasText, et2HasText, et3HasText; // Global variables
et1.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(s.length > 0){ 
            et1HasText = true;
        }else{
            et1HasText = false;
        }
        validateButton();
}
// Other callbacks
...
...
});

et2.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(s.length > 0){ 
            et2HasText = true;
        }else{
            et2HasText = false;
        }
        validateButton();
}
//Other callbacks
...
...
});

et3.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(s.length > 0){ 
            et3HasText = true;
        }else{
            et3HasText = false;
        }
        validateButton();
}
//Other callbacks
...
...

});

private void validateButton(){
    if(et1HasText && et2HasText && et3HasText){
    //Enable the button only when all the 3 edit texts have text
        button.setEnabled(true);
    }else{
    //Disable otherwise (user clicks backspace and clears the edit text etc)
        button.setEnabled(false);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM