Format TextView to look like link

前端 未结 7 1563
醉话见心
醉话见心 2021-01-04 02:21

I\'ve been using the android:autoLink just fine for formatting links and such, but I need to use android:onClick so I can\'t use that in this case.

7条回答
  •  被撕碎了的回忆
    2021-01-04 02:54

    With kotlin extension function (if you don't need the click effect as on a real link)

    fun TextView.hyperlinkStyle() {
        setText(
            SpannableString(text).apply {
                setSpan(
                    URLSpan(""),
                    0,
                    length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            },
            TextView.BufferType.SPANNABLE
        )
    }
    

    How to use

    yourTextView.hyperlinkStyle()
    

提交回复
热议问题