3

我正在尝试创建一个 TextField,在 Jetpack 中使用下划线但没有任何其他边框或背景。我该怎么做呢?

这是我目前正在使用的代码:

val query = remember {mutableStateOf("")}
        TextField(
        value = query.value,
            onValueChange = { newValue -> query.value = newValue },
            label={Text("Dummy", color = colorResource(id = R.color.fade_green))},
            textStyle = TextStyle(
                textAlign = TextAlign.Start,
                color = colorResource(id = R.color.fade_green),
                fontFamily = FontFamily(Font(R.font.poppins_regular)),
                fontSize = 14.sp,
            ),
            modifier = Modifier
                .padding(start = 30.dp).border(0.dp, Color.Red),
            colors = TextFieldDefaults.textFieldColors(
                backgroundColor = Color.Transparent
            )
            )
4

1 回答 1

3

有了1.0.0你就可以使用:

var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    label = { Text("label") },
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = Color.Transparent,
        //Color of indicator = underbar
        focusedIndicatorColor = ....,
        unfocusedIndicatorColor = ....,
        disabledIndicatorColor = ....
    )
)

在此处输入图像描述 在此处输入图像描述

如果要更改indicatorWidth当前没有内置参数。

您可以使用.drawBehind修饰符来绘制一条线。就像是:

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

val indicatorColor = if (isFocused) Color.Red else Color.Gray
val indicatorWidth = 4.dp

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(0f, y),
                Offset(size.width, y),
                strokeWidth
            )
    },
    colors = TextFieldDefaults.textFieldColors(
        backgroundColor = Color.Transparent,
        focusedIndicatorColor =  Transparent,
        unfocusedIndicatorColor = Transparent,
        disabledIndicatorColor = Transparent
    )
)

在此处输入图像描述

于 2021-04-20T07:42:13.847 回答