当我们在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. python爬虫学习视频资料免费送,用起来非常666

    当我们浏览网页的时候,经常会看到像下面这些好看的图片,你是否想把这些图片保存下载下来. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片点击鼠标右键的时候并没有另存为选项,或者你可以通过截图工 ...

  2. Android OpenGL ES 开发(四): OpenGL ES 绘制形状

    在上文中,我们使用OpenGL定义了能够被绘制出来的形状了,现在我们想绘制出来它们.使用OpenGLES 2.0来绘制形状会比你想象的需要更多的代码.因为OpenGL的API提供了大量的对渲染管线的控 ...

  3. #研发解决方案#研发协作平台CloudEngine

    Cloud Engine:大杀器如何炼成 郑昀(微博:http://weibo.com/yunzheng) 创建于2016/6/18 最后更新于2016/6/19 点击查看我的<如何从零搭建一个 ...

  4. [Swift]LeetCode86. 分隔链表 | Partition List

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. [Swift]LeetCode257. 二叉树的所有路径 | Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  6. [Swift]LeetCode328. 奇偶链表 | Odd Even Linked List

    Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...

  7. [Swift]LeetCode350. 两个数组的交集 II | Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  8. [Swift]LeetCode1027. 最长等差数列 | Longest Arithmetic Sequence

    Given an array A of integers, return the length of the longest arithmetic subsequence in A. Recall t ...

  9. 关于datagrid中数据条件颜色问题

    前天公司考核中做了一个小的考核项目,在考核中一直没找到怎么设置datagrid中数据颜色的代码 他的题目是这样的: 项目资金小于50000时,项目资金数字需要红色文字显示,否则以绿色文字显示 后来找到 ...

  10. linux运维架构师职业规划

    1.假如你从来未接触过Linux的话,首先要做的就找一本指导书来学习.现在公认的Linux的入门书籍是“鸟哥的私房菜”,讲的很全面,鸟哥的私房菜一共分为两部,一部是基础篇,一部是服务器篇.“鸟哥的私房 ...