美文网首页
监听多个EditText,改变Button按钮颜色

监听多个EditText,改变Button按钮颜色

作者: 我挺平凡 | 来源:发表于2019-07-16 22:12 被阅读0次

一、效果图

a.gif

二、创建工具类

public class WorksSizeCheckUtil {
    //发布作品时候填写尺寸的监听器
    static IEditTextChangeListener mChangeListener;

    public static void setChangeListener(IEditTextChangeListener changeListener) {
        mChangeListener = changeListener;
    }


    /**
     * 检测输入框是否都输入了内容
     * 从而改变按钮的是否可点击
     * 以及输入框后面的X的可见性,X点击删除输入框的内容
     */
    public static class textChangeListener {
        private TextView button;
        private EditText[] editTexts;

        public textChangeListener(TextView button) {
            this.button = button;
        }

        public textChangeListener addAllEditText(EditText... editTexts) {
            this.editTexts = editTexts;
            initEditListener();
            return this;
        }


        private void initEditListener() {
            Log.i("TAG", "调用了遍历editext的方法");
            for (EditText editText : editTexts) {
                editText.addTextChangedListener(new textChange());
            }
        }


        /**
         * edit输入的变化来改变按钮的是否点击
         */
        private class textChange implements 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 (checkAllEdit()) {
                    Log.i("TAG", "所有edittext有值了");
                    mChangeListener.textChange(true);
                    button.setEnabled(true);
                } else {
                    button.setEnabled(false);
                    Log.i("TAG", "有edittext没值了");
                    mChangeListener.textChange(false);
                }
            }

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

        /**
         * 检查所有的edit是否输入了数据
         *
         * @return
         */
        private boolean checkAllEdit() {
            for (EditText editText : editTexts) {
                if (!TextUtils.isEmpty(editText.getText() + "")) {
                    continue;
                } else {
                    return false;
                }
            }
            return true;
        }
    }
}

三、接口

public interface IEditTextChangeListener {
    void textChange(boolean isHasContent);
}

四、Activity代码

        //1.创建工具类对象 把要改变颜色的btn先传过去
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(mBtn);

        //2.把所有要监听的edittext都添加进去
        textChangeListener.addAllEditText(mEt1,mEt2,mEt3);

        //3.接口回调 在这里拿到boolean变量 根据isHasContent的值决定 btn 应该设置什么颜色
        WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if(isHasContent){
                    mBtn.setBackgroundResource(R.color.colorPrimary);
                }else{
                    mBtn.setBackgroundResource(R.color.colorAccent);
                }
            }
        });

相关文章

网友评论

      本文标题:监听多个EditText,改变Button按钮颜色

      本文链接:https://www.haomeiwen.com/subject/ramxlctx.html