[英]Android TextView make http and custom scheme urls clickable
我正在創建一個 android 應用程序並定義了一個活動,如下所示:
<activity
android:name="com.scheme.app.MainActivity"
android:screenOrientation="portrait"
android:theme="@style/MainActivityTheme">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="invite"
android:scheme="schemeapp" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
當點擊 url “schemeapp://invite”時,用戶將被帶到 MainActivity。
我有一個包含以下內容的 textView:
textView.setText("Testing custom scheme - schemeapp://invite with http http://www.LMNGTFY.com");
我需要使 web url ( http://www.LMNGTFY.com ) 以及自定義 url (schemeapp://invite) 可點擊。
我已經嘗試過的:
String text = "Testing custom scheme - schemeapp://invite with http http://www.LMNGTFY.com";
textView.setText(text);
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.ALL);
Pattern urlDetect = Pattern.compile("(schemeapp):\\/\\/([a-zA-Z0-9.]+)");
Matcher matcher = urlDetect.matcher(text);
String scheme = null;
while (matcher.find()) {
String customSchemedUrl = matcher.group(0);
Uri uri = Uri.parse(customSchemedUrl);
scheme = uri.getScheme();
break;
}
if (!TextUtils.isEmpty(scheme)) {
Linkify.addLinks(textView, urlDetect, scheme);
}
如果我刪除以下代碼行來檢測 Web url,則自定義方案 url 有效:
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS | Linkify.EMAIL_ADDRESSES | Linkify.ALL);
如果我嘗試將 http 添加為自定義方案 url,則 http url 不可點擊。
編輯:澄清
我不能使用 HTML,因為用戶輸入也將顯示在 TextView 上並且需要鏈接。
你能幫忙嗎? 謝謝
這是一種選擇:
final Spanned html = Html.fromHtml("Testing custom scheme - <a href='schemeapp://invite'>schemeapp://invite</a> with http http://www.LMNGTFY.com";);
helpText.setText(html);
helpText.setMovementMethod(new LinkMovementMethod());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.