码迷,mamicode.com
首页 > 移动开发 > 详细

Android EditText输入最大值提示功能

时间:2015-07-13 17:57:46      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

public class LengthFilter implements InputFilter {  
     public LengthFilter(int max) {  
         mMax = max;  
     }  
  
     @Override  
     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) {  
             if (mErrorToast == null) {  
                 mErrorToast = Toast.makeText(ShowDetailCommentActivity.this, R.string.comment_input_overflow,  
                         Toast.LENGTH_SHORT);  
             }  
             mErrorToast.show();  
             return "";  
         } else if (keep >= end - start) {  
             return null; // keep original  
         } else {  
             keep += start;  
             if (mErrorToast == null) {  
                 mErrorToast = Toast.makeText(ShowDetailCommentActivity.this, R.string.comment_input_overflow,  
                         Toast.LENGTH_SHORT);  
             }  
             mErrorToast.show();  
             if (Character.isHighSurrogate(source.charAt(keep - 1))) {  
                 --keep;  
                 if (keep == start) {  
                     return "";  
                 }  
             }  
             return source.subSequence(start, keep);  
         }  
     }  
  
     private int mMax;  
 }  
mEditText.setFilters(new InputFilter[] { new LengthFilter(mMaxTextNum) });  

代码很少但有效,各位可以尝试一下。

 

作者:xzy2046,转载需注明。博客主页:http://blog.csdn.net/xzy2046

Android EditText输入最大值提示功能

标签:

原文地址:http://www.cnblogs.com/tianzhijiexian/p/4643338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!