問題描述
我正在嘗試在類似于 Google & 的文本視圖中添加多個鏈接.Flipboard 的條款和條件和隱私政策如下面的屏幕截圖所示:
I'm trying to add multiple links in a textview similar to what Google & Flipboard has done below with their Terms and conditions AND Privacy Policy shown in the screen shot below:
到目前為止,我偶然發現了這種方法
So far I've stumbled on using this approach
textView.setText(Html.fromHtml(myHtml);textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(Html.fromHtml(myHtml); textView.setMovementMethod(LinkMovementMethod.getInstance());
myHtml 是一個 href.
where myHtml is a href.
但它并沒有給我我需要的控制權,例如啟動片段等.
But it doesn't give me control I need e.g to launch a fragment etc.
知道他們是如何在下面的兩個示例中實現這一點的嗎?
Any idea how they achieve this in the two examples below?
推薦答案
你可以使用Linkify (android.text.Spannable,java.util.regex.Pattern,java.lang.String)
String termsAndConditions = getResources().getString(R.string.terms_and_conditions);
String privacyPolicy = getResources().getString(R.string.privacy_policy);
legalDescription.setText(
String.format(
getResources().getString(R.string.message),
termsAndConditions,
privacyPolicy)
);
legalDescription.setMovementMethod(LinkMovementMethod.getInstance());
Pattern termsAndConditionsMatcher = Pattern.compile(termsAndConditions);
Linkify.addLinks(legalDescription, termsAndConditionsMatcher, "terms:");
Pattern privacyPolicyMatcher = Pattern.compile(privacyPolicy);
Linkify.addLinks(legalDescription, privacyPolicyMatcher, "privacy:");
然后您可以使用該方案來啟動一個活動,例如通過在 AndroidManifest 中添加該方案:
and then you can use the scheme to start an activity for example by adding the scheme in the AndroidManifest:
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="terms" />
<data android:scheme="privacy" />
</intent-filter>
如果您想執行自定義操作,可以將 Intent-filter 設置為您當前的活動,這將有一個 singleTop 啟動模式.
If you want to do a custom action, you can set the intent-filter to your current activity, which will have a singleTop launchmode.
這將導致 onNewIntent 在哪里被觸發可以進行自定義操作:
This will cause onNewIntent to be fired where can make your custom actions:
@Override
protected void onNewIntent(final Intent intent) {
...
if (intent.getScheme().equals(..)) {
..
}
}
這篇關于Android 上 TextView 中的多個可點擊鏈接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!