注释串的开始和结尾索引因语言而异

发布于 2025-02-07 15:55:52 字数 869 浏览 2 评论 0原文

我使用 this(不再那么好)示例启用链接在我的Android JetPack上,可以组合文本(请参阅“ ClickableText andles link on Text上”的部分)。

到目前为止,对于一种语言而言,这很容易且不错。正如您在注释串中看到的那样。Builder:

addStyle(
    style = SpanStyle(
        textDecoration = TextDecoration.Underline
    ),
    start = 8,
    end = 15
)
addStringAnnotation(
    tag = uriTag,
    annotation = "https://developer.android.com/jetpack/compose",
    start = 8,
    end = 15
)

我必须输入启动和结束索引以突出链接。想象一下我有多个字符串语言资源,我只想链接网站 webseite

 "My website" 
 "Meine Webseite"

字符串的启动和结束索引从4到10。

上部英语 拥有7至14。这是用于多种语言资源的不太可用。在不计算索引的情况下,如何更轻松地链接我的文本。

(请注意:我想使用天然库Andoridx。* Kotlin。*仅。其他第三方库将被忽略)

I used this (not so nice anymore) example to enable linkification on my Android Jetpack Compose Text composable (see section with "The ClickableText handles link on text").

That works so far easy and nice for one language. As you can see in the AnnotatedString.Builder:

addStyle(
    style = SpanStyle(
        textDecoration = TextDecoration.Underline
    ),
    start = 8,
    end = 15
)
addStringAnnotation(
    tag = uriTag,
    annotation = "https://developer.android.com/jetpack/compose",
    start = 8,
    end = 15
)

I have to enter start and end indices to highlight the link by underlines. Imagine I have multiple string language resources and I only want to linkify website or Webseite:

 "My website" 
 "Meine Webseite"

The upper english string would have start and end indices from 4 to 10.

Lower german string would have 7 to 14. This is for multiple language resources not very usable. How can I linkify my Text composable more easily, without calculating indices.

(Please note: I want to use natural libraries andoridx.* kotlin.* only. Other 3rd party libraries will be ignored)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2025-02-14 15:55:52

您可以使用以下偏移量。

onClick = { offset ->
                        annotatedText.getStringAnnotations(start = offset, end = offset)
                            .firstOrNull()?.let {
                                onLinkClick(it.item)
                            }
                    }
 

You can use offset like below.

onClick = { offset ->
                        annotatedText.getStringAnnotations(start = offset, end = offset)
                            .firstOrNull()?.let {
                                onLinkClick(it.item)
                            }
                    }
 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文