简体   繁体   中英

How to enable the button when text is entered and disable in Android java?

How to enable the button when text is entered and disable when the textfield is empty? OR When the button is clicked check the textfield if empty alert else run the func();

I just want to know which method can do the above?

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

                // TODO Auto-generated method stub
            }

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

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {
// if length greater then 1 enable the button else disable it here
                // TODO Auto-generated method stub
            }
        });

Try this code

Button btn = (Button)findViewById(R.id.buttonAddress);
EditText ed = (EditText)findViewById(R.id.EditText);

String value = ed.getText().toString

if(value.string.length() == 0 || value = null) {

ButtonName.setEnabled(false);  

}

This should work

You can use the setEnabled(boolean) function for the view of the button. After you get the object through findViewById(). You can do the rest of your functionality by using the onClickListener.

View.OnClickListener myhandler = new View.OnClickListener() {
    public void onClick(View v) {
      //todo
    }
  }

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