android:editText控件输入框输入达到上限后输入自动不计入

一、前言:我在写一个使用editText控件对输入的字数进行控制的功能

二、页面布局:

XML 复制代码
 <EditText
                android:id="@+id/remark"
                android:layout_width="319dp"
                android:layout_height="145dp"
                android:layout_marginStart="33dp"
                android:layout_marginTop="11dp"
                android:background="#EDEDED"
                android:inputType="textFilter"
                android:gravity="start"
                android:hint="输入备注信息"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView12" />

            <TextView
                android:id="@+id/num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:text="0/200"
                app:layout_constraintBottom_toBottomOf="@+id/remark"
                app:layout_constraintEnd_toEndOf="@+id/remark" />

如果要使得输入的内容达到设置的值就停止呈现就需要输入这两行内容

XML 复制代码
                android:inputType="textFilter"
java 复制代码
            binding.remark.setFilters(new InputFilter[] {new InputFilter.LengthFilter(20)});

java代码

java 复制代码
 binding.remark.setFilters(new InputFilter[] {new InputFilter.LengthFilter(20)});
            binding.remark.addTextChangedListener(new TextWatcher() {

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


                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                                              int after) {


                }

                @Override
                public void afterTextChanged(Editable s) {
                    binding.num.setText(String.valueOf(s.length()) + "/20");
                    if (s.length() >= 20) {
                        Toast.makeText(getContext(), "上限了", Toast.LENGTH_SHORT).show();
                    }

                }
            });
相关推荐
ritxgt0068 小时前
MySQL 数据增删改查
android·数据库·mysql
zlpzpl9 小时前
MySQL 的 INSERT(插入数据)详解
android·数据库·mysql
Jomurphys12 小时前
Compose 调用 - 震动 LocalHapticFeedback
android·compose
非凡ghost12 小时前
小X分身APP(手机分身类工具)
android·windows·学习·智能手机·软件需求
erqi15 小时前
Compose你入门吧
android
q***765615 小时前
MySQL 中如何进行 SQL 调优
android·sql·mysql
zhanglinping61915 小时前
MySQL——内置函数
android·数据库·mysql
m***787415 小时前
mysql之字符串函数
android·数据库·mysql
w***711016 小时前
MySQL 事务的操作和四大特性
android·数据库·mysql
松叶似针18 小时前
Flutter三方库适配OpenHarmony【secure_application】— Android 端 FLAG_SECURE 实现分析
android·flutter