免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1191 | 回复: 0
打印 上一主题 下一主题

Android editText 输入字数限制 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览

方法一:

  1. // 输入框限制输入字数  
  2.         editText.addTextChangedListener(new TextWatcher() {  
  3.             private CharSequence temp;  
  4.             private boolean isEdit = true;  
  5.             private int selectionStart ;  
  6.             private int selectionEnd ;  
  7.             @Override  
  8.             public void beforeTextChanged(CharSequence s, int arg1, int arg2,  
  9.                     int arg3) {  
  10.                 temp = s;  
  11.             }  
  12.    
  13.             @Override  
  14.             public void onTextChanged(CharSequence s, int arg1, int arg2,  
  15.                     int arg3) {  
  16.             }  
  17.    
  18.             @Override  
  19.             public void afterTextChanged(Editable s) {  
  20.                  selectionStart = editText.getSelectionStart();  
  21.                 selectionEnd = editText.getSelectionEnd();  
  22.                 Log.i("gongbiao1",""+selectionStart);  
  23.                 if (temp.length() > Constant.TEXT_MAX) {  
  24.                     Toast.makeText(KaguHomeActivity.this,  
  25.                             R.string.edit_content_limit, Toast.LENGTH_SHORT)  
  26.                             .show();  
  27.                     s.delete(selectionStart-1, selectionEnd);  
  28.                     int tempSelection = selectionStart;  
  29.                     editText.setText(s);  
  30.                     editText.setSelection(tempSelection);  
  31.                 }  
  32.             }  
  33.    
  34.    
  35.         });  

方法二:
利用EditText 可以设置filter的特性,自定义一个LengthFilter,当输入字数超过限制时 ,做出自定义的提示

  1. // 输入框限制输入字数  
  2.        InputFilter[] filters = new InputFilter[1];  
  3.        filters[0] = new InputFilter.LengthFilter(Constant.TEXT_MAX) {  
  4.            @Override  
  5.            public CharSequence filter(CharSequence source, int start, int end,  
  6.                    Spanned dest, int dstart, int dend) {  
  7.                if (source.length() > 0 && dest.length() == Constant.TEXT_MAX) {  
  8.                    if ((System.currentTimeMillis() - toastTime) > interval) {  
  9.                        toastTime = System.currentTimeMillis();  
  10.                        Toast  
  11.                                .makeText(KaguHomeActivity.this,  
  12.                                        R.string.edit_content_limit,  
  13.                                        Toast.LENGTH_SHORT).show();  
  14.                    }  
  15.                }  
  16.                if (dest.toString().equals(  
  17.                        getResources().getString(R.string.input_default_txt))) {  
  18.                    Bundle data = new Bundle();  
  19.                    data.putCharSequence("source", source);  
  20.                    Message message = textHandler.obtainMessage();  
  21.                    message.setData(data);  
  22.                    message.sendToTarget();  
  23.                }  
  24.   
  25.                return super.filter(source, start, end, dest, dstart, dend);  
  26.            }  
  27.        };  
  28.        editText.setFilters(filters);  
  29. rivate Handler textHandler = new Handler() {  
  30.        @Override  
  31.        public void handleMessage(Message msg) {  
  32.   
  33.            Bundle data = msg.getData();  
  34.            CharSequence source = data.getCharSequence("source");  
  35.            editText.setTextColor(Color.BLACK);  
  36.            editText.setText(source);  
  37.            editText.setSelection(source.length());  
  38.        }  
  39.    };  

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP