How to disable Button if EditText is empty ?

2019-01-11 20:18发布

问题:

I have an EditText and a Button in my application.

When the button is clicked,the text entered in the EditText is added to a ListView.

I want to disable the Button if the EditText is empty.How to do this ?

This is my code for button click

ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
            imb.setOnClickListener(new OnClickListener()
            {
             @Override
             public void onClick(View arg0) 
             { 
                 EditText et = (EditText)findViewById(R.id.EditText1);

                  String str = et.getText().toString();
                  web1.add(str);
                  Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
                  adapter1.notifyDataSetChanged();
                  et.setText("");

                    }
            });
            }

How can i do this ?

回答1:

    editText1.addTextChangedListener(new TextWatcher() {

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

           if(s.toString().trim().length()==0){
                button.setEnabled(false);
              } else {
                button.setEnabled(true);
              }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });


回答2:

Use TextChangedListener and initially disable ImageButton in onCreate().

Try this

public class MyActivity extends Activity {

ImageButton imb;
EditText et;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imb = (ImageButton) findViewById(R.id.btn_send);
    et = (EditText) findViewById(R.id.EditText1);

    imb.setEnabled(false); // set button disable initially

    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

            if (s.toString().equals("")) {
                imb.setEnabled(false);
            } else {
                imb.setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });

}

}


回答3:

Simple just check the condition in onCreate

if (et.getText().toString().trim().equals("")){
  button.setEnabled(false);
}
else{
button.setEnabled(true);
}


回答4:

Add a TextWatcher to your EditText, so that when you change the text inside it, you Button enables or disables itself.



回答5:

Initally in onCreate() disable the button. Then add a addTextChangedListenerto the edit text. within that check the edittext length and disable if it is 0 or otherwise enable it



回答6:

I used TextUtils for a concise solution:

    editText.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            button.setEnabled(!TextUtils.isEmpty(s.toString().trim()));
        }

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

        @Override
        public void afterTextChanged(Editable s) {
        }
    });


回答7:

on Oncreate() , before button click you should check the condition as,

   ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
   EditText et = (EditText)findViewById(R.id.EditText1);
   if(et.getText().toString().equals("")
   {
   imb.setEnabled(false);
   }

   imb.setOnClickListener(new OnClickListener()
        {
         @Override
         public void onClick(View arg0) 
         { 
             EditText et = (EditText)findViewById(R.id.EditText1);

              String str = et.getText().toString();
              web1.add(str);
              Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
              adapter1.notifyDataSetChanged();
              et.setText("");

                }
        });


回答8:

When you want to disable the editText there You will use below code

editText.setEnabled(false); 
editText.setFocusable(false);


回答9:

you check the status of an edittext at runtime using the text watcher. the below code counts the text length and disables if the length is zero. use this code:

EditText mEditText = new EditText(this);
    mEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (s.length() == 0) {
                button.setEnabled(false);
            }
                            else {
                            button.setEnabled(true);
                        }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });


回答10:

If you want to use an object oriented solution and reuse your code

public abstract class EmptyTextWatcher implements TextWatcher
{
    public abstract void onEmptyField();

    public abstract void onFilledField();

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if (s.toString().trim().length() == 0)
        {
            onEmptyField();
        } else
        {
            onFilledField();
        }
    }

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

    }



    @Override
    public void afterTextChanged(Editable s)
    {

    }

}

so you can use it just doing

textView.addTextChangedListener(new EmptyTextWatcher()
        {

            @Override
            public void onEmptyField()
            {
                button.setEnabled(false);
            }

            @Override
            public void onFilledField()
            {
                button.setEnabled(true);
            }
        });


回答11:

if anyone was wondering here is the kotlin version of the code

editText1.addTextChangedListener(object: TextWatcher {
        override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
            if (s.toString().trim({ it <= ' ' }).isEmpty())
            {
                button.setEnabled(false)
            }
            else
            {
                button.setEnabled(true)
            }
        }
        override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
                              after:Int) {
            // TODO Auto-generated method stub
        }
        override fun afterTextChanged(s: Editable) {
            // TODO Auto-generated method stub
        }
    })