如何创建一个带有下划线、没有任何背景或边框的文本字段?

Spa*_*tta 5 android android-jetpack-compose android-compose-textfield

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

这是我当前使用的代码:

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
            )
            )
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 4

首先1.2.0您可以使用BasicTextField+ TextFieldDecorationBox

就像是:

    BasicTextField(
        value = text,
        onValueChange = {text = it},
        interactionSource = interactionSource,
        textStyle = mergedTextStyle,
        enabled = enabled,
        singleLine = true,
        modifier = Modifier
            .background(
                color = White,  //Background color
                shape = TextFieldDefaults.TextFieldShape
            )
            .indicatorLine(
                enabled = enabled,
                isError = false,
                interactionSource = interactionSource,
                colors = colors,
                focusedIndicatorLineThickness =  2.dp, //width of the indicator
                unfocusedIndicatorLineThickness = 2.dp
            )
        ) {

            TextFieldDefaults.TextFieldDecorationBox(
                value = text,
                enabled = enabled,
                singleLine = true,
                innerTextField = it,
                visualTransformation = VisualTransformation.None,
                interactionSource = interactionSource,
                label = { Text("Label") },
            )

    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


通过 ,1.0.0您可以仅使用该colors属性来定义Color.Transparent背景。

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 = ....
    )
)
Run Code Online (Sandbox Code Playgroud)

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

如果您想更改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
    )
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述