Disable Button when Edit Text Fields empty

后端 未结 7 558
栀梦
栀梦 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:53

    My way was:

    1. button setEnabled(false)

    2. I used TextWatcher to listen for text change and update status of button in afterTextChanged function

    Here is my code:

    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();
        }
    };
    
    public void updateSignInButtonState() {
        btnSubmit.setEnabled(edUsername.getText().length() > 0 &&
                edPassword.getText().length() > 0);
    }
    

提交回复
热议问题