简体   繁体   English

如何更改 Android Jetpack Compose 中的 OutlineTextField 边框宽度?

[英]How change OutlineTextField border width in Android Jetpack Compose?

My code:我的代码:

OutlinedTextField(
     value = state.value,
     onValueChange = { state.value = it },
     modifier = Modifier.fillMaxWidth().padding(start = 30.dp, end = 30.dp),
     label = { Text(text = "Something", fontSize = 14.sp) },
     shape = RoundedCornerShape(12.dp),
)

I want to increase the border width so that the colors focusedBorderColor , disabledBorderColor are supported.我想增加边框宽度,以便支持 colors focusedBorderColordisabledBorderColor

You can change OutlinedTextField border like this您可以像这样更改 OutlinedTextField 边框

    var hasFocus by remember { mutableStateOf(false) }

    OutlinedTextField(
        modifier = modifier
            .border(
                width = 1.dp,
                color = if (hasFocus) Color.Red else Color.Unspecified
            )
            .onFocusChanged { focusState -> hasFocus = focusState.hasFocus },
        colors = TextFieldDefaults.outlinedTextFieldColors(
            focusedBorderColor = Color.Unspecified,
            unfocusedBorderColor = Color.Unspecified
        )
    )

Another solution is to use BaseTextField instead of OutlinedTextField另一种解决方案是使用BaseTextField而不是OutlinedTextField

Outline border is defined as a constant value in OutlinedTextField .轮廓边框在OutlinedTextField定义为常量值。

private val IndicatorUnfocusedWidth = 1.dp
private val IndicatorFocusedWidth = 2.dp

There is no direct way to override these values.没有直接的方法来覆盖这些值。

So, you have to create complete custom TextField Composables if you need to achieve dynamic border width.因此,如果您需要实现动态边框宽度,则必须创建完整的自定义 TextField Composable。

You can copy-paste the complete code in OutlinedTextField.kt and TextFieldImpl.kt and modify them as required to create the custom Composables.您可以复制粘贴OutlinedTextField.ktTextFieldImpl.kt的完整代码,并根据需要修改它们以创建自定义可组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅在 Jetpack Compose 中更改特定边的边框宽度? - How to change border width of a particular side only in jetpack compose? 如何在 Jetpack compose 中更改边框的颜色,而与它的宽度和形状无关? - How can I change border's color independent of it's width and shape in Jetpack compose? 如何更改 Jetpack Compose 中文本字段的边框颜色? - How to change border color of textfield in jetpack compose? 如何更改卡片的边框颜色,在 Jetpack Compose 中点击卡片? - How to change the border color of a card, on card click in Jetpack Compose? 如何在 Jetpack Compose 中更改 TextField 的指示器宽度? - How to change TextField's indicator width in Jetpack Compose? 如何在 Jetpack Compose 中更改行边框的形状? - How to change the shape of a Row's border in Jetpack Compose? OutlineTextField 、TextField 在 Jetpack Compose 1.0.0-alpha02 中不起作用 - OutlineTextField , TextField not working in Jetpack Compose 1.0.0-alpha02 Android Jetpack 在 Box 中组合部分边框 - Android Jetpack compose partial border in Box 如何在jetpack compose中保留内容更改的按钮宽度 - How to retain button width on content change in jetpack compose 在 Jetpack Compose 中更改 android:windowBackground - Change android:windowBackground in Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM