一行代码监听EditText变化改变Button可否点击

本文介绍了一种使用一行代码监听EditText输入变化并据此改变Button状态的方法。通过定义不同的按钮样式并在EditText内容发生变化时更新Button的背景及可用状态,实现了交互效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一行代码监听EditText变化改变Button可否点击

  • 我们要实现的效果为在未输入时显示默认的颜色且无法点击,在EditText输入后改变按钮颜色且可点击,点击后改变颜色。

    • 主要思路为:首先创建按钮变化的drawable文件,这个我就不多说了,直接上代码。
  • button_disabled.xml

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
        <solid android:color="#911b77c6" />
        <corners android:radius="5dp" />
        <stroke
            android:width="1dp"
            android:color="#910062b8" />
    </shape>
  • button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#1b77c6" />
    <corners android:radius="5dp" />
    <stroke
        android:width="1dp"
        android:color="#0062b8" />
</shape>
  • button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#0062b8" />
    <corners android:radius="5dp" />
</shape>
  • button_select.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false"
        android:drawable="@drawable/button_normal" />
    <item android:state_focused="false" android:state_pressed="true"
        android:drawable="@drawable/button_pressed" />
</selector>
  • 按钮的布局文件:

<EditText
                    android:id="@+id/et_b"
                    android:hint="0.00"
                    android:background="@null"
                    android:inputType="number"
                    android:textColorHint="#cdcfd1"
                    android:textColor="@color/back_7"
                    android:textSize="20sp"
                    android:layout_marginLeft="16dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />

<Button
            android:id="@+id/tv_btn"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/button_disabled"
            android:gravity="center"
            android:text="确定"
            android:enabled="false"
            android:textColor="@color/white"
      android:textSize="18sp" />
  • 现在我们就上主要的代码:
 public class TextChangeUtils implements TextWatcher {

    private TextView btn;
    private List<EditText> mList = new ArrayList<>();

    public TextChangeUtils(EditText editText, TextView btn) {
        mList.add(editText);
        this.btn = btn;
    }

    public TextChangeUtils(List<EditText> mList, TextView btn) {
        this.mList = mList;
        this.btn = btn;
    }

    @Override
    public void afterTextChanged(Editable arg0) {

    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {

    }

    @Override
    public void onTextChanged(CharSequence cs, int start, int before,
                              int count) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getText().length() > 0) {
                if (i + 1 == mList.size()) {
                    btn.setBackgroundResource(R.drawable.button_select);
                    btn.setEnabled(true);
                }
            } else {
                btn.setBackgroundResource(R.drawable.button_disabled);
                btn.setEnabled(false);
                break;
            }
        }
    }
}
  • 上面的代码很简单就不多说了,我定义了两个方法供调用,一个是判断单个EditText是否输入,一个是传入一个集合的EditText。现在我们来看一下如何调用。
etB.addTextChangedListener(new TextChangeUtils(etB, tvBtn));

你只需要找到你的控件传入就可实现了,是不是很简单,方法还有很多可改进的地方,望广大袁友改进。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值