首先实现的效果先看一下,知道是说的什么:
右下角就是监听我们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啦。