繁体   English   中英

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

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM