Android : Linkify 文本 onclick 没有调用网络 url
Android : Linkify text onclick is not calling the web url
在我的 android 应用程序 Activity
中有一个 TextView
带有多个可点击文本。我使用 Linkify
使它们可点击。但问题是他们不会在点击时调用网络 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>
您好,请在下面找到答案,如果有任何问题,请告诉我。
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 <a href="http://myexample.in/privacypolicy">Privacy Policy </a and agree to the <a href="http://myexample.in/termsofservice">Terms and Conditions </a.</string>
最后我解决了 problem.Use 下面的代码。
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 的末尾。例如..如果你想链接文本 "Terms and Conditions" 与“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 应用程序 Activity
中有一个 TextView
带有多个可点击文本。我使用 Linkify
使它们可点击。但问题是他们不会在点击时调用网络 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>
您好,请在下面找到答案,如果有任何问题,请告诉我。
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 <a href="http://myexample.in/privacypolicy">Privacy Policy </a and agree to the <a href="http://myexample.in/termsofservice">Terms and Conditions </a.</string>
最后我解决了 problem.Use 下面的代码。
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 的末尾。例如..如果你想链接文本 "Terms and Conditions" 与“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”.