【发布时间】:2021-05-31 05:57:30
【问题描述】:
【问题讨论】:
标签: android textfield android-jetpack-compose android-jetpack-compose-text
【问题讨论】:
标签: android textfield android-jetpack-compose android-jetpack-compose-text
使用1.0.0(使用1.0.0-beta07 测试),TextField 遵循材料指南,并且没有内置参数可以更改此行为。
您应该使用带有特定样式的BasicTextField。
否则你可以使用drawBehind修饰符:
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()
val IndicatorUnfocusedWidth = 1.dp
val IndicatorFocusedWidth = 2.dp
val TextFieldPadding = 16.dp
val indicatorColor = if (isFocused) Color.Red else Color.Gray
val indicatorWidth = if (isFocused) IndicatorFocusedWidth else IndicatorUnfocusedWidth
TextField(
value = text,
onValueChange = {
text = it },
label={Text("Label")},
interactionSource = interactionSource,
modifier = Modifier
.drawBehind {
val strokeWidth = indicatorWidth.value * density
val y = size.height - strokeWidth / 2
drawLine(
indicatorColor,
Offset(TextFieldPadding.toPx(), y),
Offset(size.width - TextFieldPadding.toPx(), y),
strokeWidth
)
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Transparent,
unfocusedIndicatorColor = Transparent,
disabledIndicatorColor = Transparent
)
)
【讨论】: