简体   繁体   English

如何在 Jetpack Compose 中更改 TextField 的指示器宽度?

[英]How to change TextField's indicator width in Jetpack Compose?

I've been wondering if there is a way to change TextField's indicator (underline) width.我一直想知道是否有办法更改 TextField 的指示符(下划线)宽度。

There isn't a parameter to achieve it since TextField follows the Material guidelines.没有实现它的参数,因为TextField遵循 Material 指南。

You can use a custom modifier to draw a line at the bottom of the TextField :您可以使用自定义修饰符在TextField的底部画一条线:

fun Modifier.drawCustomIndicatorLine(
    indicatorBorder: BorderStroke,
    indicatorPadding : Dp = 0.dp
): Modifier {

    val strokeWidthDp = indicatorBorder.width
    return drawWithContent {
        drawContent()
        if (strokeWidthDp == Dp.Hairline) return@drawWithContent
        val strokeWidth = strokeWidthDp.value * density
        val y = size.height - strokeWidth / 2
        drawLine(
            indicatorBorder.brush,
            Offset((indicatorPadding).toPx(), y),
            Offset(size.width - indicatorPadding.toPx(), y),
            strokeWidth
        )
    }
}

Then use it and apply a Transparent color to hide the default Indicator.然后使用它并应用Transparent颜色来隐藏默认指示器。

Something like:就像是:

val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()

val indicatorColor =  if (isFocused) Color.Red else Teal200

TextField(
    value = text,
    onValueChange = { text = it },
    interactionSource = interactionSource,
    modifier = Modifier.drawCustomIndicatorLine(
        indicatorBorder = BorderStroke(1.dp,indicatorColor),
        indicatorPadding = 10.dp
    ),
    shape = RoundedCornerShape(8.dp),
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = Color.LightGray,
        cursorColor = Color.Black,
        focusedIndicatorColor =  Transparent,
        unfocusedIndicatorColor = Transparent,
        disabledIndicatorColor = Transparent
    )
)

在此处输入图像描述

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

相关问题 如何在 Jetpack Compose 上更改 TextField 的突出显示文本颜色? - How to change TextField's highlighted text color on Jetpack Compose? 如何更改 Jetpack Compose 中文本字段的边框颜色? - How to change border color of textfield in jetpack compose? 如何在 Jetpack Compose 中设置行宽等于 TextField 的宽度? - How to set the width of a row to be equal to width of TextField in Jetpack Compose? Jetpack Compose:文本字段和 FAB 不使用全宽 - Jetpack Compose: Textfield and FAB not using full width 如何在 Android Jetpack Compose 的 TextField 中更改输入字体大小 - How to change the input font size in TextField of Android Jetpack Compose 如何在 Jetpack compose 中更改边框的颜色,而与它的宽度和形状无关? - How can I change border's color independent of it's width and shape in Jetpack compose? 如何在jetpack compose中的文本字段旁边添加drawable? - How to add drawable's next to textfield in jetpack compose? 在 Jetpack Compose TextField 中操作/更改键盘输入 - Manipulate/Change Keyboard Input in Jetpack Compose TextField 在 TextField (Jetpack Compose) 中更改 cursor 的大小 - Change size of cursor in TextField (Jetpack Compose) Jetpack Compose:无法更改 TextField 焦点 - Jetpack Compose: can't change TextField focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM