1

我创建了一个自定义 URL 跨度,以使用 chrome 自定义选项卡打开链接。链接显示正确,我使用该Html.fromHtml()功能。

在活动中,我将其用于 TextView:

content_txt_view = (TextView)findViewById(R.id.textView_news);
content_txt_view.setTransformationMethod(new LinkTransformationMethod());
content_txt_view.setMovementMethod(LinkMovementMethod.getInstance());

链接转换类如下所示:

public class LinkTransformationMethod implements TransformationMethod {

    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        if (view instanceof TextView) {

            TextView textView = (TextView) view;
          //  Linkify.addLinks(textView, Linkify.WEB_URLS);
            if (textView.getText() == null || !(textView.getText() instanceof Spannable)) {
                return source;
           }
            Spannable text= new SpannableString(textView.getText());
            URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class);
            for (int i = spans.length - 1; i >= 0; i--) {
                URLSpan oldSpan = spans[i];
                int start = text.getSpanStart(oldSpan);
                int end = text.getSpanEnd(oldSpan);
                String url = oldSpan.getURL();
                text.removeSpan(oldSpan);
                text.setSpan(new CustomTabsURLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
            return text;
        }
        return source;
    }

and the custom url span:

public class CustomTabsURLSpan extends URLSpan {
    private Context context;


    public CustomTabsURLSpan(String url) {

        super(url);
        Log.d("SensibleUrlSpan", "1");
    }

    public CustomTabsURLSpan(Parcel src) {
        super(src);
        Log.d("SensibleUrlSpan", "2");
    }

    @Override
    public void onClick(View widget) {
        Log.d("SensibleUrlSpan", "3");
        String url = getURL();
        Toast toast = Toast.makeText(context, "well done! you click ", Toast.LENGTH_SHORT);
        toast.show();
        // String url = "http://www.google.com";

    }
}

我本来希望当我单击链接时,我会收到 toast 消息……但似乎根本没有调用 OnClick 方法。

4

1 回答 1

0

下面的类实现了所需的行为:

CustomClickURLSpan.java

import android.text.style.URLSpan;
import android.view.View;

public class CustomClickURLSpan extends URLSpan {
    private OnClickListener mOnClickListener;

    public CustomClickURLSpan(String url) {
        super(url);
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        mOnClickListener = onClickListener;
    }

    @Override
    public void onClick(View widget) {
        if (mOnClickListener == null) {
            super.onClick(widget);
        } else {
            mOnClickListener.onClick(widget, getURL());
        }
    }

    public interface OnClickListener {
        void onClick(View view, String url);
    }
}

CustomTabsOnClickListener.java

import android.app.Activity;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.view.View;

import java.lang.ref.WeakReference;

public class CustomTabsOnClickListener implements CustomClickURLSpan.OnClickListener {
    private WeakReference<Activity> mActivityWeakReference;
    private WeakReference<CustomTabActivityHelper> mCustomTabActivityHelperWeakReference;

    public CustomTabsOnClickListener(Activity hostActivity,
                                     CustomTabActivityHelper customTabActivityHelper) {
        mActivityWeakReference = new WeakReference<>(hostActivity);
        mCustomTabActivityHelperWeakReference = new WeakReference<>(customTabActivityHelper);
    }

    @Override
    public void onClick(View view, String url) {
        Activity activity = mActivityWeakReference.get();
        CustomTabActivityHelper customTabActivityHelper =
                mCustomTabActivityHelperWeakReference.get();
        if (activity != null && customTabActivityHelper != null) {
            CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder(null)
                    .build();
            customTabsIntent.intent.setPackage(
                    CustomTabsHelper.getPackageNameToUse(view.getContext()));
            customTabsIntent.launchUrl(activity, Uri.parse(url));
        }
    }
}

具有 linkifyUrl 方法的实用程序类,可为 URL 创建 Span

import android.text.Spannable;
import android.text.Spanned;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Util {
    private static final Pattern URL_PATTERN = Pattern.compile("((http|https|rstp):\\/\\/\\S*)");

    public static void linkifyUrl(
            Spannable spannable, CustomClickURLSpan.OnClickListener onClickListener) {
        Matcher m = URL_PATTERN.matcher(spannable);
        while (m.find()) {
            String url = spannable.toString().substring(m.start(), m.end());
            CustomClickURLSpan urlSpan = new CustomClickURLSpan(url);
            urlSpan.setOnClickListener(onClickListener);
            spannable.setSpan(urlSpan, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

最后,调用这些类来链接文本:

TextView content = (TextView)findViewById(R.id.content);
Spannable spannable = new SpannableString(post.getText());
Util.linkifyUrl(spannable, new CustomTabsOnClickListener(this, mCustomTabActivityHelper));
content.setText(spannable);
content.setMovementMethod(LinkMovementMethod.getInstance());

使用的帮助类可以在 Github Demo 上找到:CustomTabsHelperCustomTabsActivityHelper

于 2018-04-05T19:41:36.783 回答