如果任何EditText为空,如何禁用按钮?

时间:2018-05-28 09:23:01

标签: java android button android-edittext

我尝试过使用TextWatcher,但是如果我创建多个TextWatchers(每个EditText一个),当我开始填写任何EditText时,该按钮会启用。

示例:

public class MainActivity extends AppCompatActivity {

    EditText numberOneEditText;
    EditText numberTwoEditText;
    EditText numberThreeEditText;
    TextView sumTextView;

    public void calculate(View view) {


        String numberOneString = numberOneEditText.getText().toString();
        String numberTwoString = numberTwoEditText.getText().toString();
        String numberThreeString = numberThreeEditText.getText().toString();

        double numberOneDouble = Double.parseDouble(numberOneString);
        double numberTwoDouble = Double.parseDouble(numberTwoString);
        double numberThreeDouble = Double.parseDouble(numberThreeString);

        double sum = numberOneDouble + numberTwoDouble + numberThreeDouble;

        sumTextView.setText(sum.getText().toString())

   }



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

        numberOneEditText = findViewById(R.id.numberOneEditText);
        numberTwoEditText = findViewById(R.id.numberTwoEditText);
        numberThreeEditText = findViewById(R.id.numberThreeEditText);
        sumTextView = findViewById(R.id.sumTextView);

    }
}

5 个答案:

答案 0 :(得分:1)

使用TextWatcher。它将解决您的问题:

PGM

答案 1 :(得分:1)

这必须解决您的问题:

public class AppActivity extends Activity {

    private EditText editText1;
    private EditText editText2;

    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            checkFieldsForEmptyValues();
        }

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);

        editText1 = (EditText) findViewById(R.id.reg_password);
        editText2 = (EditText) findViewById(R.id.reg_password2);

        editText1.addTextChangedListener(textWatcher);
        editText2.addTextChangedListener(textWatcher);

        checkFieldsForEmptyValues();

    }

    private  void checkFieldsForEmptyValues(){
        Button btn = (Button) findViewById(R.id.btnId);

        String s1 = editText1.getText().toString();
        String s2 = editText2.getText().toString();

        if (s1.length() > 0 && s2.length() > 0) {
            btn.setEnabled(true);
        } else {
            btn.setEnabled(false);
        }
    }
}

感谢。

答案 2 :(得分:0)

您只需声明一个分配给所有edittexts的textwatcher。然后:

  btn.setEnabled(!(str1.isEmpty || str2.isEmpty() || str3.isEmpty));

您也可以直接检查它是否是有效的双倍,如果没有则不启用。

答案 3 :(得分:0)

您的文本观察者应检查所有三个EditTexts是否都有数据(最好还确保它们是双倍的,如果不是则显示带有错误消息的标签)

这样的事情:

  TextWatcher watcher = new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence s, int start, int 
 count, int after) {
      }
      @Override
      public void onTextChanged(CharSequence s, int start, int before, 
int count) {
      }

      @Override
      public void afterTextChanged(Editable s) {
          for (EditText et  : new EditText[] {numberOneEditText, 
numberTwoEditText, numberThreeEditText}) {
          try {
            Double.parseDouble(et.getText());
          } catch (NumberFormatException e) {
            // Disable button, show error label, etc.
            button.setEnabld(false);
            return;
          }
        }
       //Enable buttton, hide error label if available
       button.setEnabled(true);
      }
    };
}
numberOneEditText.addTextChangedListener(watcher);
numberTwoEditText.addTextChangedListener(watcher);
numberThreeEditText.addTextChangedListener(watcher);

答案 4 :(得分:0)

        EditText numberOneEditText;
        EditText numberTwoEditText;
        EditText numberThreeEditText;
        TextView sumTextView;

        public void calculate(View view) {

                boolean isValid = true;
                String numberOneString = numberOneEditText.getText().toString();
                String numberTwoString = numberTwoEditText.getText().toString();
                String numberThreeString = numberThreeEditText.getText().toString();
                if(numberOneString.trim().isEmpty ||numberTwoString.trim().isEmpty() 
             || numberThreeString.trim().isEmpty()){
              isValid = false;}
              if(isValid){
                double numberOneDouble = Double.parseDouble(numberOneString);
                double numberTwoDouble = Double.parseDouble(numberTwoString);
                double numberThreeDouble = Double.parseDouble(numberThreeString);

                    double sum = numberOneDouble + numberTwoDouble + numberThreeDouble;

                    sumTextView.setText(sum.getText().toString())
    }  

}