Disable Button when Edit Text Fields empty

后端 未结 7 510
栀梦
栀梦 2020-12-31 04:22

I have two text fields and a Button. I want to disable the Button unless both EditText-Fields are not empty. I tried many solutions here at stackoverflow, but they don\'t wo

7条回答
  •  礼貌的吻别
    2020-12-31 04:54

    You may resolve this problem a much shorter:

    @Override
    protected void onResume() {
        super.onResume();
        TextWatcher tw = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    
            @Override
            public void afterTextChanged(Editable editable) {
               updateSignInButtonState();
            }
        };
    
        editLogin.addTextChangedListener(tw);
        editPassword.addTextChangedListener(tw);
    

    }

    private void updateSignInButtonState() {
        buttonSignIn.setEnabled(editLogin.getText().length() > 0 &&
                                editPassword.getText().length() > 0);
    }
    

提交回复
热议问题