效果图:
监听" border="0" alt="监听" src="/Upload/Images/2013101618/79D8CD816B455387.png" width="244" height="79" />
主要代码:
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
uploadTitle = mEditText.getText().toString().trim();
String length = uploadTitle.length() + "/" + maxTitle;
if (uploadTitle.length() > maxTitle) {
//防止Toast被输入键盘遮挡,将Toast显示在屏幕中间
ToastUtil.MidToast(getActivity(),
"已超过" + (uploadTitle.length() - maxTitle) + ""
+ "个字!");
//如果字数超出了限定,则将其颜色变为红色
SpannableStringBuilder builder = new SpannableStringBuilder(
length);
builder.setSpan(new ForegroundColorSpan(Color.RED), 0, 2,
Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv_upload_titlelength.setText(builder);
} else {
tv_upload_titlelength.setText(length);
}
}