简体   繁体   中英

How to disable/enable button when the edit text is empty/nonempty

I have 2 edit text and 1 button. How can I make this button to be disabled until the user fill all the edit text? I used this code but when I run this code it always disable the button even when I fill the 2 Edit text, also I do not know what the onTextChanged and beforeTextChanged doing!!

Please help me, I will appreciate it.

public class TestActivity extends Activity {
/** Called when the activity is first created. */
EditText edit1;
EditText edit2;
EditText edit3;
Button button;
String test1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // Your initialization code...
    // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            edit1= (EditText) findViewById(R.id.edit1);
            edit2= (EditText) findViewById(R.id.edit2);

            button=(Button) findViewById(R.id.button);




   TextWatcher watcher = new LocalTextWatcher();
    edit1.addTextChangedListener(watcher);
    edit2.addTextChangedListener(watcher);

    updateButtonState();
}

void updateButtonState() {
    boolean enabled;
    if(enabled = checkEditText(edit1)
        && checkEditText(edit2)){
        button.setEnabled(enabled);}
        }
}

private boolean checkEditText(EditText edit) {
    return Integer.getInteger(edit1.getText().toString()) != null;
}

private class LocalTextWatcher implements TextWatcher {
    public void afterTextChanged(Editable s) {
        updateButtonState();
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
}




}}}

Monerah change your checkEditText method for:

private boolean checkEditText(EditText edit) {
    return edit.getText().length() == 0;
}

and your updateButtonState() for:

void updateButtonState() {
    if(checkEditText(edit1) && checkEditText(edit2)) button.setEnabled(false);
    else button.setEnabled(true);
}

That would make it work right.

As an additional advice, I would change checkEditText's name for isEditTextEmpty or something more representative to what it does. It would make the if statement much more readable :)

Regarding your question on what the onTextChanged and beforeTextChanged methods do, take a look at the following:

beforeTextChanged(CharSequence s, int start, int count, int after). This means that the characters are about to be replaced with some new text. The text is uneditable. Use: when you need to take a look at the old text which is about to change.

onTextChanged(CharSequence s, int start, int before, int count). Changes have been made, some characters have just been replaced. The text is uneditable. Use: when you need to see which characters in the text are new.

afterTextChanged(Editable s). The same as above, except now the text is editable. Use: when you need to see and possibly edit new text.

The first two methods are of no use to what you are trying to do so with afterTextChanged you are done. Hope it helps.

Actually, any of these methods can be used to accomplish enabling/disabling a button according to what is in a text field:

@Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
    if (charSequence.length() == 0 && after > 0) {
        // enable button
    } else if (charSequence.length() == count && after == 0) { 
        // disable button
    }
}

// OR

@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
    if (charSequence.length() == count && before == 0) {
        // enable button
    } else if (charSequence.length() == 0 && before > 0)  
        // disable button
    }
}

// OR

@Override
public void afterTextChanged(Editable editable) {
    if (editable.length() > 0 && !button.isEnabled()) {
        // enable button
    } else if (editable.length() == 0 && btnEnroll.isEnabled()) {
        // disable button 
    }
}

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