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());
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值