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


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

    二、页面布局:

    1. <EditText
    2. android:id="@+id/remark"
    3. android:layout_width="319dp"
    4. android:layout_height="145dp"
    5. android:layout_marginStart="33dp"
    6. android:layout_marginTop="11dp"
    7. android:background="#EDEDED"
    8. android:inputType="textFilter"
    9. android:gravity="start"
    10. android:hint="输入备注信息"
    11. app:layout_constraintStart_toStartOf="parent"
    12. app:layout_constraintTop_toBottomOf="@+id/textView12" />
    13. <TextView
    14. android:id="@+id/num"
    15. android:layout_width="wrap_content"
    16. android:layout_height="wrap_content"
    17. android:layout_alignParentRight="true"
    18. android:layout_alignParentBottom="true"
    19. android:text="0/200"
    20. app:layout_constraintBottom_toBottomOf="@+id/remark"
    21. app:layout_constraintEnd_toEndOf="@+id/remark" />

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

                    android:inputType="textFilter"
    
                binding.remark.setFilters(new InputFilter[] {new InputFilter.LengthFilter(20)});
    

    java代码

    1. binding.remark.setFilters(new InputFilter[] {new InputFilter.LengthFilter(20)});
    2. binding.remark.addTextChangedListener(new TextWatcher() {
    3. @Override
    4. public void onTextChanged(CharSequence s, int start, int before, int count) {
    5. }
    6. @Override
    7. public void beforeTextChanged(CharSequence s, int start, int count,
    8. int after) {
    9. }
    10. @Override
    11. public void afterTextChanged(Editable s) {
    12. binding.num.setText(String.valueOf(s.length()) + "/20");
    13. if (s.length() >= 20) {
    14. Toast.makeText(getContext(), "上限了", Toast.LENGTH_SHORT).show();
    15. }
    16. }
    17. });

  • 相关阅读:
    CSS 媒体查询 @media【详解】
    java计算机毕业设计学生健康信息管理源程序+mysql+系统+lw文档+远程调试
    Redis事务
    目标检测算法
    图像变视频
    c 语言开发
    数据库导入文字不乱吗 CSV
    C++ Map常见用法说明
    AttributeError: 'NoneType' object has no attribute 'int4WeightExtractionHalf'
    【uniapp+vue3+ts】请求函数封装,请求和上传文件拦截器
  • 原文地址:https://blog.csdn.net/Rssqzqyp/article/details/133645873