Enable and disable Button according to the text in EditText in Android

后端 未结 3 1061
盖世英雄少女心
盖世英雄少女心 2020-12-17 14:59

I want to disable by Button if the words in the EditText is less than 3 words, and if the words in the EditText are more than 3 words then I want to enable it so that it can

3条回答
  •  情话喂你
    2020-12-17 15:37

    You have to addTextChangedListener to your EditText

    Like this:

    yourEditText.addTextChangedListener(new TextWatcher() {
          @Override
          public void afterTextChanged(Editable arg0) {
             enableSubmitIfReady();
          }
    
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {
          }
    
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
          }
        });
    

    In that method, you should do like this:

     public void enableSubmitIfReady() {
    
        boolean isReady = yourEditText.getText().toString().length() > 3;    
        yourbutton.setEnabled(isReady);
      }
    

    Hope it helps.

提交回复
热议问题