Android对EditText进行字数的监听并且实施更改

首先实现的效果先看一下,知道是说的什么:


右下角就是监听我们EditText字数的一个TextView,将TextView 的背景变成这个图片Ok啦。


首先,说一下这部分的布局文件xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="190dp"
    android:layout_marginTop="10dp">

    <EditText
        android:id="@+id/edit_special_sign"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:background="@drawable/clear_edit_select"
        android:gravity="top"
        android:maxLength="30"
        android:padding="15dp"
        android:textColor="@color/edit_text_color_select"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/text_special_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="10dp"
        android:layout_marginRight="15dp"
        android:background="@mipmap/num"
        android:gravity="center"
        android:text="30" />
</RelativeLayout>

然后TextView就是存放我们这个数字的容器。根据你输入的字符的个数,对我们的数字进行改变,具体如下:

edit_special_sign.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        int number = 30 - s.length();
        text_special_num.setText("" + number);

    }
});
关键代码就是这些了,对我们的edittext添加TextChanged的监听,然后在after中写两行就ok啦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值