鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > 手机系统开发 > android > >

监听EditText中的字数,并且超出字数时显示红色字体

来源:互联网 作者:佚名 时间:2013-10-23 09:16
效果图: 主要代码: mEditText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { uploadTitle = mEditText.getText().toString().trim(); String length = uploadTit

效果图:

主要代码:

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);
                }
            }

网友评论
<