简体   繁体   中英

Android LinkMovementMethod in XML?

To enable Linkify behavior, I do this in code:

textView.setMovementMethod(LinkMovementMethod.getInstance());

Is there a way to set this in the layout xml for the textview ?

No, there is not. If you are using setMovementMethod(LinkMovementMethod.getInstance()); in so many places in your app that you want to avoid it, you should consider creating and using a custom class extending TextView which executes that method.

Use the "autoLink" property in the XML layout

In kotlin you can create an extension function like this

@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
    text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml("<a href='${url}'>${text}</a>")
    }
    movementMethod = LinkMovementMethod.getInstance()
    isClickable = true
}

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