Android : Linkify text onclick is not calling the web url

天涯浪子 提交于 2019-12-12 04:23:08

问题


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?

Below is my code.

    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");

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>

回答1:


Hi Please find the answer below let me know if any issues.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    txtLinkfy.setText(Html.fromHtml(getString(R.string.licence_text), Html.FROM_HTML_MODE_LEGACY));
} else {
    txtLinkfy.setText(Html.fromHtml(getString(R.string.licence_text)));
}
txtLinkfy.setMovementMethod(LinkMovementMethod.getInstance());

Define a string in your strings.xml

<string name="licence_text">By tapping to continue, you are indicating that you have read the &lt;a href="http://myexample.in/privacypolicy">Privacy Policy &lt;/a and agree to the &lt;a href="http://myexample.in/termsofservice">Terms and Conditions &lt;/a.</string>



回答2:


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);

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".



来源:https://stackoverflow.com/questions/42803555/android-linkify-text-onclick-is-not-calling-the-web-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!