适用于 Android 的 AutoLink 撰写文本 [英] AutoLink for Android Compose Text

查看:31
本文介绍了适用于 Android 的 AutoLink 撰写文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 JetPack Compose Text 上使用 android:autoLink 功能?

Is there any way to use android:autoLink feature on JetPack Compose Text?

我知道,这可能不是声明式方式";在一个简单的标签/修饰符中使用此功能,但也许有一些简单的方法?

I know, that it is maybe not "declarative way" for using this feature in one simple tag/modifier, but maybe there is some easy way for this?

对于样式文本,我可以使用这种方式

For styling text I can use this way

 val apiString = AnnotatedString.Builder("API provided by")
        apiString.pushStyle(
            style = SpanStyle(
                color = Color.Companion.Blue,
                textDecoration = TextDecoration.Underline
            )
        )
        apiString.append("https://example.com")

        Text(text = apiString.toAnnotatedString())

但是,我如何管理此处的点击次数?如果我以编程方式说出我期望系统的行为(电子邮件、电话、网络等),那就太好了.喜欢它.与 TextView 一起使用.谢谢

But, how can I manage clicks here? And would be great, if I programatically say what behaviour I expect from the system (email, phone, web, etc). Like it. works with TextView. Thank you

推荐答案

我们可以实现Linkify 类型的 TextView 如下例所示,

We can achieve Linkify kind of TextView in Android Compose like this example below,

@Composable
fun LinkifySample() {
    val uriHandler = UriHandlerAmbient.current

    val layoutResult = remember {
        mutableStateOf<TextLayoutResult?>(null)
    }

    val text = "API provided by"
    val annotatedString = annotatedString {
        pushStyle(
            style = SpanStyle(
                color = Color.Companion.Blue,
                textDecoration = TextDecoration.Underline
            )
        )
        append(text)
        addStringAnnotation(
            tag = "URL",
            annotation = "https://example.com",
            start = 0,
            end = text.length
        )
    }
    Text(
        fontSize = 16.sp,
        text = annotatedString, modifier = Modifier.tapGestureFilter { offsetPosition ->
            layoutResult.value?.let {
                val position = it.getOffsetForPosition(offsetPosition)
                annotatedString.getStringAnnotations(position, position).firstOrNull()
                    ?.let { result ->
                        if (result.tag == "URL") {
                            uriHandler.openUri(result.item)
                        }
                    }
            }
        },
        onTextLayout = { layoutResult.value = it }
    )
}

在上面的例子中,我们可以看到我们给出了文本,并且我们使用 addStringAnnotation 来设置标签.并且使用tapGestureFilter,我们可以得到点击的注解.

In the above example, we can see we give the text and also we use addStringAnnotation to set the tag. And using tapGestureFilter, we can get the clicked annotation.

最后使用 UriHandlerAmbient.current 我们可以打开电子邮件、电话或网络等链接.

Finally using UriHandlerAmbient.current we can open the link like email, phone, or web.

参考:https://www.hellsoft.se/rendering-markdown-with-jetpack-compose/

这篇关于适用于 Android 的 AutoLink 撰写文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆