Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563037
  • 博文数量: 192
  • 博客积分: 3780
  • 博客等级: 中校
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:11
文章存档

2012年(6)

2011年(160)

2010年(26)

分类: 嵌入式

2011-07-08 11:05:53

方法一:

  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.    };  

阅读(484) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~