使用textview中的自定义方案进行网址制作

时间:2018-03-15 21:54:44

标签: android

我的网址包含TextView中显示的自定义方案。问题是,当我尝试使用像Linkify这样的东西时,整个文本部分都是可点击的。我正在关注此link以尝试使其正常运行,但链接仅在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是链接。

1 个答案:

答案 0 :(得分:0)