Android:Linkify文本onclick未调用网址 [英] Android : Linkify text onclick is not calling the web url

查看:87
本文介绍了Android:Linkify文本onclick未调用网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序Activity中,有一个TextView,其中包含多个可点击的文本.我已经使用Linkify使它们可单击.但是问题在于,他们不会在点击时调用Web URL.我该如何解决?

In my android application Activity there is a TextView with multiple clickable text for. I have used Linkify for making them clickable. But the problem is that they don't call the web URL on click. How do I solve this?

下面是我的代码.

    String termsAndConditions = "Terms and Conditions";
    String privacyPolicy = "Privacy Policy";

    txtLinkfy.setText(
            String.format(
                    "By tapping to continue, you are indicating that " +
                            "you have read the Privacy Policy and agree " +
                            "to the Terms and Conditions.",
                    termsAndConditions,
                    privacyPolicy)
    );
    txtLinkfy.setMovementMethod(LinkMovementMethod.getInstance());

    Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
    Linkify.addLinks(txtLinkfy, termsAndConditionsMatcher,"http://myexample.in/termsofservice");
    Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
    Linkify.addLinks(txtLinkfy, privacyPolicyMatcher, "http://myexample.in/privacypolicy");

在AndroidManifest.xml中:

And in AndroidManifest.xml :

    <activity android:name=".view.ActivityContinue"
              android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="Terms and Conditions" />
            <data android:scheme="Privacy Policy" />
        </intent-filter>
    </activity>

推荐答案

最后我解决了我的问题.请使用下面的代码.

Finally I solved my problem.Use below code.

Linkify.TransformFilter transformFilter = new Linkify.TransformFilter() {
        public final String transformUrl(final Matcher match, String url) {
            return "";
        } };

    Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
    Linkify.addLinks(txtLinkfy,termsAndConditionsMatcher,"http://myexample.in/termsofservice",null,transformFilter);
    Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
    Linkify.addLinks(txtLinkfy,privacyPolicyMatcher,"http://myexample.in/privacypolicy",null,transformFilter);

API"Linkify.addLinks"的行为是这样,它将您要链接的字符串附加到URL的末尾.例如,如果您想将文本条款和条件"与" http://myexample.in/termsofservice".最终的URL是" http://myexample.in/termsofserviceTerms 和条件",这不是我所需要的.所以我用了api transformFilter返回一个空字符串.因此,我的最终网址是" http://myexample.in/termsofservice ".

The behavior of API "Linkify.addLinks" is such that, it appends the string that you want to linkify to the end of the url. For ex..if you want to linkify text "Terms and Conditions" with "http://myexample.in/termsofservice". The final URL is "http://myexample.in/termsofserviceTerms and Conditions" which is not what I needed.So i used the api transformFilter return a null string. So my final url is "http://myexample.in/termsofservice".

这篇关于Android:Linkify文本onclick未调用网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆