【问题标题】:Android TextView make http and custom scheme urls clickableAndroid TextView 使 http 和自定义方案 url 可点击
【发布时间】:2016-08-05 12:19:30
【问题描述】:

我正在创建一个 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 上并且需要链接。

你能帮忙吗? 谢谢

【问题讨论】:

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


    【解决方案1】:

    这是一种选择:

    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());
    

    【讨论】:

    • 谢谢,可以,但是我不能放 html,textview 也会显示用户的输入,所以他们不会使用 HTML 标签。
    • 您可以在此之前对文本进行预处理,例如 userinput.replace("schemeapp://.*..","
    • 在我们的 iPhone 应用上测试同样的内容,因为内容是跨平台的 ..
    • 当然可以,但你可以在设置为 textview 之前在 android 上进行预处理
    • 我不得不做一些处理,但这是让它在所有场景中工作的唯一方法。 @ligi 感谢合作伙伴的努力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-09
    • 1970-01-01
    相关资源
    最近更新 更多