textview上部分文字有链接。如何实现。

有时候购买产品的时候。需要同意某些协议。只有协议部分的文字有颜色,并且有链接效果。如何实现。

效果如图所示:

上代码。在activity文件里面。 init方法就是设置textview上面的文字《》和《》之间的内容为超链接。其中new TextClick()是设置这段超链接文字的监听器。TextClick类的updateDrawState方法里面可以设置超链接文字的颜色。

  private void init() {
        String content = "已阅读并同意《增值服务:xxx产品服务协议》";
        SpannableStringBuilder spannable = new SpannableStringBuilder(content);
        int startIndex = 0;
        int endIndex = 0;
        if (!TextUtils.isEmpty(content)) {
            //获取位置
            startIndex = content.indexOf("《");
            endIndex = content.lastIndexOf("》") + 1;
        }
        spannable.setSpan(new TextClick(), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //这个一定要记得设置,不然点击不生效。
        tvLookXieyi.setMovementMethod(LinkMovementMethod.getInstance());
        tvLookXieyi.setText(spannable);
    }
    /**** 超链接文字点击事件*/
    private class TextClick extends ClickableSpan {
        @Override
        public void onClick(View view) {
            //你的业务需求,比如点击跳转到h5页面
            if(OtherUtils.isFastDoubleClick()) {
                return;
            }
            Intent intent = new Intent(HuiYuanPayActivity.this, WebviewActivity.class);
            intent.putExtra("link", Constants.HuiYuanGouMaiXieYi+"qymc="+mHyspGmCrCs.getQymc()+"&spmc="+mHyspGmCrCs.getSpmc()+"&spkssj="+mHyspGmCrCs.getSpkssj()+"&ddzj="+mHyspGmCrCs.getDdzj()+"&cityName="+mHyspGmCrCs.getSsdsString());
            intent.putExtra("title", "会员购买协议");
            startActivity(intent);
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);

            ds.setColor(getResources().getColor(R.color.huiyuan_blue));   //设置字体颜
            ds.setUnderlineText(false); //设置没有下划线
        }
    }

 

本文地址:https://blog.csdn.net/liu13722785488/article/details/107493971

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐