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

                }
            });
相关推荐
alexhilton9 小时前
MVI 让屏幕一次只有一个状态
android·kotlin·android jetpack
二流小码农14 小时前
鸿蒙开发:证书只能创建3个,第4个应用怎么办
android·ios·harmonyos
阿杰m16 小时前
Android源码第十七章:PowerManager 与后台任务——锁屏后怎么跑?
android
JMchen12316 小时前
【Android 性能优化实战 60 讲】10 Jetpack App Startup 内核魔改:手写异步懒加载启动框架,根治串行初始化耗时
android·性能优化·源码分析·启动优化·kotlin协程·app startup
2601_9679801117 小时前
python+appium自动化测试如何控制App的启动和退出
android·自动化测试·python·appium·设备调试
booboosui18 小时前
不写安装器:点一下就往对话里塞 152 个字符
android
一条破秋裤18 小时前
【1117】【内存资源管控】【笔记】
android·笔记
AI导出鸭PC端19 小时前
用 AI 导出鸭高效处理:豆包导 pdf 怎么压缩但清晰
android·人工智能·pdf·豆包·ai导出鸭
therese_1008620 小时前
流媒体协议RTMP推流demo-安卓端
android
疯华整猫20 小时前
同一个 App,五种手机五种死法:2026 年 Android 五大 ROM 兼容的 8 个真实坑
android·性能优化