In Android, how to make Login button disable with respect to EditText?

前端 未结 9 2191
猫巷女王i
猫巷女王i 2020-12-28 09:50

If EditText is empty then Login Button has to be disabled. And if EditText has some texts then Login Button has to be ena

9条回答
  •  旧巷少年郎
    2020-12-28 10:26

    private TextWatcher mPhoneNumberEtWatcher = 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) {
            if (charSequence.length() >= 10) {
                mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone_activate));
    
                if (mPasswordEt.getText().toString().length() >= 5) {
                    mLoginBtn.setEnabled(true);
                }
    
            } else {
                mPhoneImg.setImageDrawable(getResources().getDrawable(R.drawable.phone));
                mLoginBtn.setEnabled(false);
            }
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
        }
    };
    
    mPhoneNumberEt.addTextChangedListener(mPhoneNumberEtWatcher);
    

    You should use TextWatcher. That will call method after typing of user. And you can check length and somthing else of your edit text.

提交回复
热议问题