Andoid EditText文本内容长度限制

可用于对输入的文本内容进行自定义验证
  这里,我是要验证EditText中用逗号分隔的每一个tag的长度不超过15个字节
tagEdt.addTextChangedListener(new TextWatcher()
    		  {
    			private CharSequence temp;
    			private int editStart ;
    			private int editEnd ;

				@Override
				public void afterTextChanged(Editable s) {
					// TODO Auto-generated method stub
					editStart = tagEdt.getSelectionStart();
					editEnd = tagEdt.getSelectionEnd();
					Log.v("afterTextChanged","editStart="+String.valueOf(editStart));
					Log.v("afterTextChanged","editEnd="+String.valueOf(editEnd));
					String nowTags = s.toString();
					String[] tags = nowTags.split(",");
					for(int i=0 ; i < tags.length;i++)
					{
						if(tags[i].length()>15)
						{
							tags[i] = tags[i].substring(0, 14);
							s.delete(editStart-1, editEnd);
							int tempSelection = editStart;
							tagEdt.setText(s);
							tagEdt.setSelection(tempSelection);

						}
					}	
				}

				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub
					temp=s;
				}
				
				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub
				}
    		  });

你可能感兴趣的:(EditText)