/** * 繼承自TextView, 用來顯示指定文本框當前輸入的字數限制 */ public class EditTextLengthIndicate extends TextView{ private EditText mEditText; private int mMaxLength; public void bindEditText(EditText et, int maxLength) { mEditText = et; mMaxLength = maxLength; setText(et.getText().length() + "/" + mMaxLength); if(et != null && maxLength > 0) { et.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub String str = s.toString(); int length = str.length(); setText(length + "/" + mMaxLength); } }); } } }
InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if ( !Character.isLetterOrDigit(source.charAt(i)) && !Character.toString(source.charAt(i)) .equals("_") && !Character.toString(source.charAt(i)) .equals("-")) { return ""; } } return null; } };