Android EditText限制输入字数

		//监听输入电话的位数
		editTel.addTextChangedListener(new TextWatcher() {
			 private CharSequence temp;//监听前的文本  
		       private int editStart;//光标开始位置  
		       private int editEnd;//光标结束位置  
		       private final int charMaxNum = 11; //最大文本数
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				temp = s;
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				editStart = editTel.getSelectionStart();
				editEnd = editTel.getSelectionEnd() ;
				if (temp.length() > charMaxNum) { 
					Toast.makeText(mApp, "您已经输入了11位电话号码了", 0).show();
					s.delete(editStart - 1, editEnd);
					int tempSelection = editStart;
					editTel.setText(s);
					editTel.setSelection(tempSelection);
				}
				
			}
		});

你可能感兴趣的:(android)