Android:链接文本OnClick没有调用Web URL



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

以下是我的代码。

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

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

最后我解决了我的问题。

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末尾的字符串。对于ex..如果您想用" http://myexample.in/termsofsofservice"将文本"条款和条件"链接。最终的URL是" http://myexample.in/termsofserviceterms and Restifes",这不是我所需的。因此,我的最终URL是" http://myexample.in/termsofsofservice"。

嗨,请找到下面的答案,让我知道是否有任何问题。

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

在您的字符串中定义字符串。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>

最新更新