为什么聚焦颜色不适用于 OutlinedTextField DecorationBox?

问题描述 投票:0回答:1

我正在尝试自定义 Jetpack Compose 中 OutlinedTextField 的外观,使其具有青色文本颜色和 1dp 粗细边框。尽管设置了适当的参数,文本字段仍保持黑色且边框不变。任何人都可以提供有关如何正确实施这些自定义的指导,特别是关于焦点颜色吗?


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MyTextField(
    value: String,
    interactionSource: InteractionSource = remember { MutableInteractionSource() },
    onValueChange: (String) -> Unit
) {
    BasicTextField(
        value = value,
        singleLine = true,
        onValueChange = onValueChange,
        decorationBox = { innerTextField ->
            OutlinedTextFieldDefaults.DecorationBox(
                value = value,
                innerTextField = innerTextField,
                enabled = true,
                singleLine = true,
                colors = OutlinedTextFieldDefaults.colors(
                    cursorColor = Color.Cyan,
                    focusedTextColor = Color.Cyan,
                    focusedBorderColor = Color.Cyan,
                ),
                interactionSource = interactionSource,
                visualTransformation = VisualTransformation.None,
                container = {
                    OutlinedTextFieldDefaults.ContainerBox(
                        enabled = true,
                        isError = false,
                        interactionSource = interactionSource,
                        colors = OutlinedTextFieldDefaults.colors(
                            cursorColor = Color.Cyan,
                            focusedTextColor = Color.Cyan,
                            focusedBorderColor = Color.Cyan,
                        ),
                        shape = RoundedCornerShape(8.dp),
                        focusedBorderThickness = 1.dp,
                        unfocusedBorderThickness = 1.dp
                    )
                }
            )
        }
    )
}

android android-jetpack-compose textfield
1个回答
0
投票

这里有一个更简单的方法。

val focusedColor = Color.Cyan
val borderColor = Color.Cyan
val borderWidth = 1.dp

OutlinedTextField(
    modifier = Modifier
        .border(
            BorderStroke(
                width = borderWidth,
                color = borderColor
            ),
            shape = RoundedCornerShape(8.dp)
        ),
    value = value,
    onValueChange = onValueChange,
    singleLine = true,
    colors = OutlinedTextFieldDefaults.colors(
        disabledTextColor = focusedColor,
        cursorColor = focusedColor,
        focusedBorderColor = borderColor,
        unfocusedBorderColor = borderColor,
        focusedLabelColor = focusedColor,
        unfocusedLabelColor = focusedColor
    ),
)
© www.soinside.com 2019 - 2024. All rights reserved.