简体   繁体   中英

Make url with custom scheme clickable in textview

I have urls with custom schemes that are displayed in a TextView . The problem is that when I try using something like Linkify not the whole text section is clickable. I am following this link to try to get it working but the link is only on google.com

Code copied from link but I am using 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)
    }

the output is: (custom://www.google.com) where only google.com is a link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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