EditText根据是否填写内容改变按钮

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_28261207/article/details/84315642

Activity中 :

// 底部按钮根据是否全部输入内容改变颜色和背景
EditStatusCheck.textChangeListener textChangeListener =
        new EditStatusCheck.textChangeListener(tvIdentityCommit);
textChangeListener.addAllEditText(etIdentityName, etIdentityNo);
EditStatusCheck.setChangeListener(new IEditTextChangeListener() {
    @Override
    public void textChange(boolean isHasContent) {
        if (isHasContent) {
            tvIdentityCommit.setBackgroundResource(R.drawable.bg_org_radius8);
        } else {
            tvIdentityCommit.setBackgroundResource(R.drawable.bg_gray_radius8);
        }
    }
});

用到的工具类

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;

public class EditStatusCheck {

    // 发布作品时候填写尺寸的监听器
    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;
        }
    }
}
展开阅读全文

没有更多推荐了,返回首页