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();
                    }

                }
            });
相关推荐
驜鸈8 分钟前
MySQL 的EXPLAIN 计划 type 字段详细说明
android·数据库·mysql
二流小码农33 分钟前
鸿蒙开发:wrapBuilder传递参数
android·ios·harmonyos
ljx14000525501 小时前
推荐一个基于Koin, Ktor & Paging等组件的KMM Compose Multiplatform项目
android·kotlin
lrydnh2 小时前
数据库语句
android·数据库
去看全世界的云3 小时前
【Kotlin】Kotlin基础笔记
android·java·笔记·kotlin
tangweiguo030519873 小时前
Android打造易用的 WiFi 工具类:WifiUtils 封装实践
android·java·wifi
Legendary_0083 小时前
LDR6500:革新手机OTG充电体验的关键芯片
android·智能手机
今阳4 小时前
鸿蒙开发笔记-11-LazyForEach 数据懒加载
android·华为·harmonyos
pengyu4 小时前
系统化掌握Flutter组件之Transform:空间魔法师
android·flutter·dart
岸芷漫步4 小时前
retrofit框架分析
android