Android:链接文字onclick不会调用网址

时间:2017-03-15 07:33:16

标签: android linkify

在我的Android应用程序Activity中,有一个TextView,其中包含多个可点击的文字。我使用Linkify来使它们可以点击。但问题是他们不会在点击时调用网址。我该如何解决这个问题?

以下是我的代码。

    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中:

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

2 个答案:

答案 0 :(得分:0)

嗨如果有任何问题,请在下面找到答案。

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

在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>

答案 1 :(得分:0)

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

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&#34; Linkify.addLinks&#34;的行为就是这样,它将你想要链接的字符串附加到url的末尾。例如,如果您想要链接文字&#34;条款和条件&#34;用&#34; http://myexample.in/termsofservice&#34;。最终的网址是&#34; http://myexample.in/termsofserviceTerms和条件&#34;这不是我需要的。所以我使用api transformFilter返回一个空字符串。所以我的最终网址是&#34; http://myexample.in/termsofservice&#34;。