当我们在Editext输入内容的时候,检测如果超过限制的长度无法输入内容,并且给用户提示。

首先我想到了下面的方法:

 editText.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) {
if (s.length() > 6){ //判断EditText中输入的字符数是不是已经大于6
editText.setText(s.toString().substring(0,6)); //设置EditText只显示前面6位字符
editText.setSelection(6);//让光标移至末端
Toast.makeText(MainActivity.this, "输入字数已达上限",
Toast.LENGTH_SHORT).show();
return;
}
}
@Override
public void afterTextChanged(Editable s) {
}
});

未经测试,个人觉得这种体验或许不是很好,或许会出现EdiText闪动。

 

其实我们可以用下面这种方法:

源码给Editext设置了过滤器,专门用来判断是否超出最大的字符长度,把这段过滤器取出来,在里面加上Toast提示,设置给Editext就可以了。

  class MyLengthFilter implements InputFilter {

        private final int mMax;
private Context context; public MyLengthFilter(int max, Context context) {
mMax = max;
this.context = context;
} public CharSequence filter(CharSequence source, int start, int end, Spanned dest,
int dstart, int dend) {
int keep = mMax - (dest.length() - (dend - dstart));
if (keep <= 0) {
//这里,用来给用户提示
Toast.makeText(context, "字数不能超过" + mMax, Toast.LENGTH_SHORT).show();
return "";
} else if (keep >= end - start) {
return null; // keep original
} else {
keep += start;
if (Character.isHighSurrogate(source.charAt(keep - 1))) {
--keep;
if (keep == start) {
return "";
}
}
return source.subSequence(start, keep);
}
} /**
* @return the maximum length enforced by this input filter
*/
public int getMax() {
return mMax;
}
}

调用:

mEditext.setFilters(new InputFilter[]{new MyLengthFilter(100,context)});      

 

源码中该方法是这样的:

public void setFilters(InputFilter[] filters) {
if (filters == null) {
throw new IllegalArgumentException();
} mFilters = filters; if (mText instanceof Editable) {
setFilters((Editable) mText, filters);
}
}

由此可见我们可以给Editext设置多个过滤器,例如过滤掉一些特殊字符,表情等等。

 

学习android有一个很重要的能力就是阅读源码的能力,很多问题都可以通过阅读源码找到解决方法。

参考链接:https://blog.csdn.net/qq_26411333/article/details/51647888

EditText超出字数限制,给用户提示的更多相关文章

  1. Android EditText输入字数限制总结(包含中文输入内存溢出的解决方法)

    转载请注明,大飞:http://blog.csdn.net/rflyee/article/details/38856539 限定EditText输入个数的解决方式非常多,可是一般主要考虑两点.也就是处 ...

  2. rhel5 新建用户提示:the home directory already exists.

    rhel5 新建用户提示:the home directory already exists.(as4不存在这个问题) 环境如下: [oracle@rhel5 ~]$ df -hFilesystem  ...

  3. linux使用su切换用户提示 Authentication failure的解决方法& 复制文件时,报cp: omitting directory `XXX'

    linux使用su切换用户提示 Authentication failure的解决方法:这个问题产生的原因是由于ubtun系统默认是没有激活root用户的,需要我们手工进行操作,在命令行界面下,或者在 ...

  4. 增加samba用户提示Failed to add entry for user

    1.首先在Ubuntu安装好samba,具体步骤为:安装samba:sudo apt-get install samba安装smbclient:sudo apt-get install 安装smbfs ...

  5. useradd umask报错 root用su 切换到普通用户提示输入密码并报密码错误

    添加新用户与以下文件有关联: /etc/default/useradd [root@localhost pam.d]# cat /etc/default/useradd # useradd defau ...

  6. android技巧:EditText输入错误时该怎样提示用户

    验证用户输入内容(EditText)应该及时准确的告诉用户,那么在Android系统中提示用户通常有以下做法: 1) 使用Toast提示 1 Toast.makeText(this, "邮箱 ...

  7. 【移动开发】EditText输入字数限制总结(包括中文输入内存溢出的解决方法)

    限定EditText输入个数的解决方案很多,但是一般主要考虑两点,也就是处理两件事:(1)不同语言字符(英文.中文等)处理方式(2)输入字符达到数目后,是否仍然允许用户输入 第一点,涉及的东东其实蛮多 ...

  8. Linux 使用 su 切换用户提示 Authentication Failure 的解决方法

    Ubuntu v14.04,使用 su 命令切换用户时报验证失败的错误 这个问题产生的原因是由于 ubuntu 系统默认是没有激活 root 用户的,需要我们手工进行操作,在命令行界面下,或者在终端中 ...

  9. Oracle12c创建新用户提示公共用户名或角色无效

    今天将备份的数据库还原到一台新的电脑上,首先要创建用户,执行如下语句: create user fxhy identified " default tablespace USERS temp ...

随机推荐

  1. AIO系列文档(1)----图解ByteBuffer

    因何而写 网上关于bytebuffer的文章真的很多,为何在此还要写一篇呢?主要是基于以下几点考虑 很多人在使用t-io时,还不会bytebuffer,只会照着t-io提供的例子照猫画虎,不利于灵活运 ...

  2. JAVA基础—适配器设计模式

    适配器概念 在计算机编程中,适配器模式将一个类的接口适配成用户所期待的.使用适配器,可以使接口不兼容而无法在一起工作的类协调工作,做法是将类自己包裹在一个已经存在的类中. JDK对适配器设计模式的应用 ...

  3. [Swift]LeetCode90. 子集 II | Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. [Swift]LeetCode234. 回文链表 | Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Example 1: Input: 1->2 Output: false ...

  5. [Swift]LeetCode1005. K 次取反后最大化的数组和 | Maximize Sum Of Array After K Negations

    Given an array A of integers, we must modify the array in the following way: we choose an i and repl ...

  6. shell 问题备忘

    一 ls结果赋给变量 dirSrc=$(ls test/ -l | awk '/^d/{print $NF}') echo "dirSrc is $dirSrc" 二 使用cut查 ...

  7. Python数据挖掘(爬虫强化)

    (我喜欢雨天,因为雨天我可以回到童年踩水花!哈!) 2018年 --7月--12日 : 多云又暴雨 T—T 前言 我要把爬虫的终极利器介绍一下,这个只要是我们肉眼能看到的,就算在源码中或者在json中 ...

  8. Git漏洞允许任意代码执行(CVE-2018-17456)复现

    Git漏洞允许任意代码执行(CVE-2018-17456) 国外安全研究员 joernchen 在 9 月 23 日向 git 官方报告了漏洞的相关细节.10月5日,Git项目披露了一个漏洞,编号为C ...

  9. Java生成全局唯一ID代码演示

    看了GitHub上的两个生成唯一ID的算法程序(一个出自百度,一个出自美团),打算运行着试试看,至于原理什么的文档上讲得很详细了,此处不再一一粘贴了,此处只演示代码 https://github.co ...

  10. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...