使具有自定义方案的 url 在文本视图中可单击



我有在TextView中显示的自定义方案的网址。问题是,当我尝试使用类似Linkify的东西时,不是整个文本部分都是可点击的。我正在点击此链接以尝试使其正常工作,但该链接仅在google.com

从链接复制的代码,但我使用的是 Kotlin:

    val fullString = "This sentence contains a custom://www.google.com custom scheme url"
    mTextView.text = fullString
    val urlDetect = Pattern.compile("([a-zA-Z0-9]+):\/\/([a-zA-Z0-9.]+)") // this is a terrible regex, don't use it. There are better url regexs.
    val matcher = urlDetect.matcher(fullString)
    var scheme: String? = null
    while (matcher.find()) {
        val customSchemedUrl = matcher.group(0)
        val uri = Uri.parse(customSchemedUrl)
        // Now you could create an intent yourself...
        // ...or if you want to rely on Linkify keep going
        scheme = uri.getScheme()
        break
    }
    if (!TextUtils.isEmpty(scheme)) {
        Linkify.addLinks(mTextView, urlDetect, scheme)
    }

输出为:(custom://www.google.com(,其中只有 google.com 是链接。

也许可点击的跨度在这里会有所帮助?

https://developer.android.com/reference/android/text/style/ClickableSpan.html

最新更新