Android之旅:android一个textView加载不同颜色的字并且响应不同的点击事件

效果图

喏,上面的就是效果图… 是不是第一个反应就是:md,so 简单

              <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />
             </LinearLayout>

但是这样肯定不可以啊~
1是麻烦
2是客户要求评论不换行…

那怎么用呢?

主要有2个思路,
1. 用html,textview有一个非常厉害的属性,setText(Html.fromText())

        private String getTextWithHtml(String from, String to, String content) {
            if (TextUtils.isEmpty(to)) {
                return "<html><body><font color=\"#33b5e5\">" + from + ":</font>" + content + "</body></html>";
            } else {
                return "<html><body><font color=\"#33b5e5\">" + from + ":</font>" + "回复:" + "<font color=\"#33b5e5\">" + to + ":</font>" + content + "</body></html>";
            }
        }

但是不同的点击事件呢?要求我们点击人名是调转到用户主页,而点击文本就是进行回复。

所以我们要使用另外一个方法:

      private SpannableString getTextWithSpan(String from, String to, String content) {
            if (TextUtils.isEmpty(to)) {
                return new SpannableString(from + ":" + content);
            } else {
                return new SpannableString(from + "回复" + to + ":" + content);
            }
        }

private class Clickable extends ClickableSpan {
        private final View.OnClickListener mListener;
        private boolean isColor;

        public Clickable(View.OnClickListener l, boolean isColor) {
            mListener = l;
            this.isColor = isColor;
        }
  /**
         * 重写父类点击事件
         */
        @Override
        public void onClick(View v) {
            mListener.onClick(v);
        }

        /**
         * 重写父类updateDrawState方法  我们可以给TextView设置字体颜色,背景颜色等等...
         */
        @Override
        public void updateDrawState(TextPaint ds) {
            if (isColor) {
                ds.setColor(context.getResources().getColor(R.color.blue));
            } else {

            }
        }
    }

 if (myCommBean != null) {
            if (TextUtils.isEmpty(myCommBean.getToAccountName())) {
                spannableString = getTextWithSpan(myCommBean.getFromAccountName(), null, myCommBean.getContent());
            } else {
                spannableString = getTextWithSpan(myCommBean.getFromAccountName(), myCommBean.getToAccountName(), myCommBean.getContent());
            }
            spannableString.setSpan(new Clickable(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getRecItemClick() != null) {
                        getRecItemClick().onItemClick(position, myCommBean, IS_HEAD, holder);
                    }
                }
            }, true), 0, myCommBean.getFromAccountName().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            spannableString.setSpan(new Clickable(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (getRecItemClick() != null) {
                        if (TextUtils.isEmpty(myCommBean.getToAccountName())) {
                            getRecItemClick().onItemClick(position, myCommBean, IS_REPLY_COMMENTS, holder);
                        } else {
                            getRecItemClick().onItemClick(position, myCommBean, IS_REPLY_REPLY, holder);
                        }
                    }
                }
            }, false), myCommBean.getFromAccountName().length(), spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.tvCommentName.setText(spannableString);
            holder.tvCommentName.setMovementMethod(LinkMovementMethod.getInstance());
        }

猜你喜欢

转载自blog.csdn.net/weixin_28717693/article/details/81811813