Android TextView 使 http 和自定义方案 url 可点击

标签 android android-intent linkify custom-scheme-url

我正在创建一个 Android 应用程序并定义了一个 Activity ,如下所示:

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

当点击网址“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());

关于Android TextView 使 http 和自定义方案 url 可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38789097/

相关文章:

android - 使用 Android KeyStore 存储用户身份验证凭据

android - 如何在Android中测试基于Espresso的Intent?

android - NestedScrollView、SwipeRefreshLayout 和WebView 如何一起使用?

android - Admob真实广告发布后不显示

android - 高级android布局: making a ListView divider from a layer-list

android - 您可以从 ActionBar 的下拉导航中启动 Activity Intent 吗

Java,基于额外字符串更改ContentView

android - 我可以在使用 Linkify 后更改 TextView 链接文本吗?

android - 警告对话框 Linkify 但保持文本为白色

android - 如何使 EditText 中的链接可点击?