Android 快速实现隐私协议跳转链接

首先在string.xml创建对应字串

  <string name="link">我已仔细阅读并同意<annotation value="privacy_policy">《隐私政策》annotation><annotation value="service_agreement">《服务协议》annotation>string>

然后在java代码中初始化Checkbox

private void initCheckBox() {
        CheckBox checkBox = findViewById(R.id.cb);
        //1、创建 SpannableStringBuilder
        SpannableStringBuilder sb = new SpannableStringBuilder(getText(R.string.link));
        //2、获取Annotation数组
        Annotation[] annotations = sb.getSpans(
                0, sb.length(), Annotation.class);
         //3、遍历 Annotation 数组
        for (Annotation annotation : annotations) {
            String annotationValue = annotation.getValue();
            //注意:这里的value就是xml中  做到一一对应即可
            if (annotationValue.equals(USER_AGREEMENT) || annotationValue.equals(PRIVACY_AGREEMENT)) {
                int start = sb.getSpanStart(annotation);
                int end = sb.getSpanEnd(annotation);
                //4、构建ClickableSpan
                ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        String url = "https:www.baidu.com";
                        switch (annotationValue) {
                            case USER_AGREEMENT:
                                url = "https:www.baidu.com";
                                break;google
                            case PRIVACY_AGREEMENT:
                                url = "https:www.google.com";
                                break;
                        }
                        //5、根据annotationValue跳转对应url
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.parse(url), "*/*");
                        startActivity(intent);
                    }

                    @Override
                    public void updateDrawState(TextPaint ds) {
                        ds.setColor(Color.BLUE);
                        //显示链接下划线
                        ds.setUnderlineText(true);
                    }
                };
                sb.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        checkBox.setText(sb);
        //关键一步,决定链接是否可以点击
        checkBox.setMovementMethod(LinkMovementMethod.getInstance());
    }

Android 快速实现隐私协议跳转链接_第1张图片


拓展—从聊天信息中解析链接

    private void initLinkText() {
        TextView tvLink = findViewById(R.id.tv_link);
        String linkStr = "跳转百度https://www.baidu.com或者跳转google https://www.google.com";
        SpannableStringBuilder urlSpannableStringBuilder = createdUrlSpannableStringBuilder(linkStr);
        tvLink.setText(urlSpannableStringBuilder);
        //设置链接颜色
        tvLink.setLinkTextColor(Color.BLUE);
        //关键一步,决定链接是否可以点击
        tvLink.setMovementMethod(LinkMovementMethod.getInstance());
    }
 public SpannableStringBuilder createdUrlSpannableStringBuilder(String linkStr){
        Matcher m = Pattern.compile("(((https|http)?://)?([a-z0-9]+[.])|(www.))"
                + "\\w+[.|\\/]([a-z0-9]{0,})?[[.]([a-z0-9]{0,})]+((/[\\S&&[^,;\u4E00-\u9FA5]]+)+)?([.][a-z0-9]{0,}+|/?)").matcher(linkStr);
        SpannableStringBuilder sb = new SpannableStringBuilder(linkStr);
        //1、用正则找出链接
        while(m.find()){
            String url = m.group();
            //2、构造URLSpan
            URLSpan urlSpan = new URLSpan(url);
            sb.setSpan(urlSpan,m.start(),m.end(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return sb;
    }

Android 快速实现隐私协议跳转链接_第2张图片

你可能感兴趣的:(android)