【问题标题】:Android : Linkify text onclick is not calling the web urlAndroid:Linkify 文本 onclick 没有调用 web url
【发布时间】:2017-03-15 07:33:16
【问题描述】:

在我的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");

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

【问题讨论】:

    标签: android linkify


    【解决方案1】:

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

    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>
    

    【讨论】:

    • 这可能有效,但我喜欢使用 Linkify 方法,而不是使用带 href 的链接。
    【解决方案2】:

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

    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“L​​inkify.addLinks”的行为是这样的,它将您要链接的字符串附加到 url 的末尾。例如……如果您想将文本“条款和条件”与“http://myexample.in/termsofservice”链接起来。最终的 URL 是“http://myexample.in/termsofserviceTerms 和条件”,这不是我需要的。所以我使用 api transformFilter 返回一个空字符串。所以我的最终网址是“http://myexample.in/termsofservice”。

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 2012-01-28
      • 2020-04-15
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-24
      相关资源
      最近更新 更多