如何在枚举课程中使用不同种类的占位符

发布于 2025-02-06 07:09:04 字数 1141 浏览 1 评论 0原文

在枚举类中,是否有可能为不同种类的占位持有人启用它吗?我想使用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

enter image description here

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

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

发布评论

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

评论(2

二智少女猫性小仙女 2025-02-13 07:09:04

StringResource()为占位符值vararg。因此,您使用:

stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders)

例如,我使用空曲线活动模板在Android Studio Chipmunk中创建了一个废料项目。我将strings.xml设置为:

<resources>
  <string name="app_name">My Application</string>
  <string name="size_placeholder">Size %1$d</string>
  <string name="sizes_placeholder_and_placeholder">Sizes %1$d and %2$d</string>
  <string name="item_a">Item A</string>
  <string name="item_B">Item B</string>

</resources>

我设置mainActivity.kt be:

package com.commonsware.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.commonsware.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
                    Content()
                }
            }
        }
    }
}

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, 1337),
    ItemB(R.string.item_B, R.string.sizes_placeholder_and_placeholder, 0, 2);
}

@Preview
@Composable
fun Content() {
    val listState = rememberLazyListState()

    LazyColumn(
        state = listState
    ) {
        items(listOf(Clothes.ItemA, Clothes.ItemB)) {
            Column {
                Text(text = stringResource(id = it.nameId))
                Text(text = stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders))
            }
        }
    }
}

结果,在缺乏所需的格式的同时,匹配所需的内容:

“上述综合”的输出

stringResource() takes a vararg for placeholder values. So, you use:

stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders)

For example, I created a scrap project in Android Studio Chipmunk, using the Empty Compose Activity template. I set up strings.xml to be:

<resources>
  <string name="app_name">My Application</string>
  <string name="size_placeholder">Size %1$d</string>
  <string name="sizes_placeholder_and_placeholder">Sizes %1$d and %2$d</string>
  <string name="item_a">Item A</string>
  <string name="item_B">Item B</string>

</resources>

And I set up MainActivity.kt to be:

package com.commonsware.myapplication

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.commonsware.myapplication.ui.theme.MyApplicationTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                // A surface container using the 'background' color from the theme
                Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) {
                    Content()
                }
            }
        }
    }
}

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, 1337),
    ItemB(R.string.item_B, R.string.sizes_placeholder_and_placeholder, 0, 2);
}

@Preview
@Composable
fun Content() {
    val listState = rememberLazyListState()

    LazyColumn(
        state = listState
    ) {
        items(listOf(Clothes.ItemA, Clothes.ItemB)) {
            Column {
                Text(text = stringResource(id = it.nameId))
                Text(text = stringResource(id = it.sizeId, it.onePlaceholder, it.twoPlaceholders))
            }
        }
    }
}

The result, while lacking your desired formatting, matches the desired content:

Output of above composable

笑叹一世浮沉 2025-02-13 07:09:04

如果我理解您的问题,您想定义某种类型的特定分组,并彼此区分。

无论如何,我将使用密封类或一个具有不同参数的类的简单对象varargs占位符字符串的简单对象。

sealed class Clothes {
  data class FixedSizeClothing(val size: String, val placeholder: String): Clothes()

  data class MultiSizeClothing(val sizes: List<String>, val placeholders: 
  List<String>): Clothes()
}

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 arguments varargs of placeholder strings.

sealed class Clothes {
  data class FixedSizeClothing(val size: String, val placeholder: String): Clothes()

  data class MultiSizeClothing(val sizes: List<String>, val placeholders: 
  List<String>): Clothes()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文