如何在枚举课程中使用不同种类的占位符
在枚举类中,是否有可能为不同种类的占位持有人启用它吗?我想使用1个具有1个占位符的项目,然后使用另一个具有2个占位符的物品。我目前的代码似乎只允许我使用1位占位符。
strings.xml
<string name="size_placeholder">Size %1$d</string>
<string name="sizes_placeholder_and_placeholder">Sizes %1$d and %2$d</string>
mainActivity.kt
enum class Clothes(@StringRes val nameId: Int, val sizeId: Int, val onePlaceholder: Int, val twoPlaceholders: Int) {
ItemA(R.string.item_a, R.string.size_placeholder, 8),
ItemB(R.string.item_B, R.string.sizes_placeholder_and_placeholder, 0, 2);
}
...
LazyColumn(
state = listState,
modifier = Modifier.weight(1f)
.padding(it)
) {
items(items) {
Column() {
Text(text = stringResource(id = it.nameId))
Text(text = stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders))
}
}
}
预期结果
Is it possible within an enum class to enable it for different kinds of placeholders? I want to use 1 item that has 1 placeholder, then another item that has 2 placeholders. The current code I have seems to only allow me to use 1 placeholder.
strings.xml
<string name="size_placeholder">Size %1$d</string>
<string name="sizes_placeholder_and_placeholder">Sizes %1$d and %2$d</string>
MainActivity.kt
enum class Clothes(@StringRes val nameId: Int, val sizeId: Int, val onePlaceholder: Int, val twoPlaceholders: Int) {
ItemA(R.string.item_a, R.string.size_placeholder, 8),
ItemB(R.string.item_B, R.string.sizes_placeholder_and_placeholder, 0, 2);
}
...
LazyColumn(
state = listState,
modifier = Modifier.weight(1f)
.padding(it)
) {
items(items) {
Column() {
Text(text = stringResource(id = it.nameId))
Text(text = stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders))
}
}
}
Expected result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
StringResource()
为占位符值vararg
。因此,您使用:例如,我使用空曲线活动模板在Android Studio Chipmunk中创建了一个废料项目。我将
strings.xml
设置为:我设置
mainActivity.kt
be:结果,在缺乏所需的格式的同时,匹配所需的内容:
stringResource()
takes avararg
for placeholder values. So, you use:For example, I created a scrap project in Android Studio Chipmunk, using the Empty Compose Activity template. I set up
strings.xml
to be:And I set up
MainActivity.kt
to be:The result, while lacking your desired formatting, matches the desired content:
如果我理解您的问题,您想定义某种类型的特定分组,并彼此区分。
无论如何,我将使用
密封类
或一个具有不同参数的类的简单对象varargs
占位符字符串的简单对象。If I understand your question, you wanted to define a specific groupings of a some type and have them some distinction from each other.
Regardless, I would approach this with
sealed classes
or a simple object of a class with varying argumentsvarargs
of placeholder strings.