@Override public void onTextChanged(CharSequence s, int start, int before, int count) { String bankId = ""; for (int i = 0; i < s.length(); i++) { if ((i + 1) % 4 == 0) { bankId += " "; } bankId += s.charAt(i); } bankcardnum.setText(bankId); Log.v( "TextWatcher-onTextChanged-->", " text=" + s.toString() + " length=" + s.length() + " start=" + start + " before-" + before + " count=" + count); }
@Override public void afterTextChanged(Editable s) { Log.v("TextWatcher-afterTextChanged-->", s.toString() + "length" + s.length()); // 输入的时候,只有一个光标,那么这两个值应该是相等的。。。 editStart = et_user_ban.getSelectionStart(); editEnd = et_user_ban.getSelectionEnd(); // 限定EditText只能输入19个数字,并且达到19个的时候用红色显示 if (charSequence.length() > 19) { Toast.makeText(AddBankCardActivity.this, "你输入的字数已经超过了限制,不能再输入!", Toast.LENGTH_SHORT) .show(); // 默认光标在最前端,所以当输入第19个数字的时候,删掉(光标位置从11-1到11)的数字,这样就无法输入超过19个以后的数字 s.delete(editStart - 1, editEnd); // 当输入超过第19个数字的时候,改变字体颜色为红色 } }
@Override public void beforeTextChanged(CharSequence ss, int start, int count, int after) { charSequence = ss; Log.v( "TextWatcher-beforeTextChanged-->", " text=" + ss.toString() + " length=" + ss.length() + " start=" + start + " count-" + count + " after=" + after); }