监听EditText中的字数,并且超出字数时显示红色字体_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 监听EditText中的字数,并且超出字数时显示红色字体

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

 2013/10/16 18:40:55  风景线外  博客园  我要评论(0)
  • 摘要:效果图:主要代码:mEditText.addTextChangedListener(newTextWatcher(){           @Override           publicvoidonTextChanged(CharSequences,intstart
  • 标签:监听

效果图:

监听" 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);
                }
            }

发表评论
用户名: 匿名