首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jetpack编写文本( color =color.*)更改颜色

Jetpack编写文本( color =color.*)更改颜色
EN

Stack Overflow用户
提问于 2022-09-19 08:25:38
回答 1查看 306关注 0票数 -1

我有两个单选按钮来改变文本的文本颜色(红色文本,硬编码)。

但是我不能让文本(color= Color.colorsTextRadio)工作。

我知道,这是一个字符串,但我得到谁的红色或绿色转换成颜色。

如果我做了一些可能会更好的事情,请告诉我,因为我是初学者。

代码语言:javascript
复制
 @Composable
fun MainScreen() {

    /**
     * Text
     */
    var text by remember {
        mutableStateOf("test")
    }

    // Event handler
    val onTextChange = { value: String ->
        text = value
    }

    /**
     * Colors
     */

    val colors = listOf("Red", "Green")

    var colorsTextRadio by remember {
        mutableStateOf(colors[0])
    }
    // Event Handler
    val onTextColorChange = { value: String ->
        colorsTextRadio = value
    }

    Log.d("TAG", "MainScreen: colorsTextRadio $colorsTextRadio")


    Column(modifier = Modifier.padding(6.dp)) {
        TextField(value = text, onValueChange = onTextChange)

        Text(text = text.replace("\n", " "), maxLines = 1, color = Color.Red)

        RadioButtonGroup(colors = colors, colorsTextRadio = colorsTextRadio, onClick = onTextColorChange)

    }
}
代码语言:javascript
复制
@Composable
fun RadioButtonGroup(
    colors: List<String>,
    colorsTextRadio: String,
    onClick: (String) -> Unit
) {
    Column(modifier = Modifier.selectableGroup()) {
        colors.forEach { label ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(56.dp)
                    .selectable(
                        selected = (colorsTextRadio == label),
                        onClick = { onClick.invoke(label) },
                        role = Role.RadioButton
                    )
                    .padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(
                    modifier = Modifier.padding(end = 16.dp),
                    selected = (colorsTextRadio == label),
                    onClick = null // null recommended for accessibility with screen readers
                )
                Text(text = label)
            }
        }
    }
}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-19 08:40:44

您可以定义数据类:

代码语言:javascript
复制
data class ColorLabel(
    val label: String,
    val color: Color
)

val colors = listOf(
    ColorLabel("Red",Red),
    ColorLabel("Green", Green))

var colorsTextRadio by remember { mutableStateOf(colors[0].label) }
var colorsText by remember { mutableStateOf(colors[0].color) }

然后将它们应用到您的可合成材料上:

代码语言:javascript
复制
  Text(text = text.replace("\n", " "),
        maxLines = 1,
        color = colorsText)

    Column(modifier = Modifier.selectableGroup()) {
        colors.forEach { colorlabel->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(56.dp)
                    .selectable(
                        selected = (colorsTextRadio == colorlabel.label),
                        onClick = {
                            colorsTextRadio = colorlabel.label
                            colorsText = colorlabel.color
                                  },
                        role = Role.RadioButton
                    )
                    .padding(horizontal = 16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(
                    modifier = Modifier.padding(end = 16.dp),
                    selected = (colorsTextRadio == colorlabel.label),
                    onClick = null // null recommended for accessibility with screen readers
                )
                Text(text = colorlabel.label)
            }
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73770502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档