根据的EditText在Android上的文本启用和禁用按钮按钮、文本、根据、Android

2023-09-06 03:56:00 作者:何必自作多情

我想禁用通过按钮,如果在的EditText的话不到3个字,如果在EditText上的话3个以上的话那么我想启用它,使它能获得点击。

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 get Clicked.

谁能帮助我在这里?

推荐答案

您必须 addTextChangedListener 您EdiText

You have to addTextChangedListener to your EdiText

这样的:

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.