39.android EditText的监听,改变按钮状态 颜色

//EditText的监听
mEd.addTextChangedListener(watcher);
//监听输入框软键盘 达到8位就变颜色
private TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        if (s.length()==8){
            //当输入到8位时自动关闭软键盘
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mEd.getWindowToken(),0);
            //到8位时是蓝色
            mButton.setBackgroundColor(Color.parseColor("#499aec"));
        }else {
            //没到8位是灰色
            mButton.setBackgroundColor(Color.parseColor("#adb4ba"));
        }
    }
};

//在布局里设置输入框固定长度是:

android:maxLength="8"

猜你喜欢

转载自blog.csdn.net/weixin_42061754/article/details/81874357
今日推荐