如何删除Androidx Compose Material中TextField的指示线?

Tri*_*ity 8 android android-textinputlayout android-jetpack-compose android-compose-textfield

我想删除 TextField 的紫色线条/指示器(见下图)。这是可能的还是我应该创建自己的自定义 TextField 来实现这一目标?

在此处输入图片说明

vik*_*mar 8

这在最近的 Jetpack Compose UI Beta 版本1.0.0-beta01 中已更改,现在您可以通过

具有所需颜色的TextFieldDefaults

 colors = TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            backgroundColor = Color.LightGray,
        )
Run Code Online (Sandbox Code Playgroud)

例子

TextField(
        value = searchText,
        onValueChange = { Log.d(HOME_COMPONENT, it) },
        label = { Text(text = "Search") },
        shape = RoundedCornerShape(10.dp),
        leadingIcon = {
            Image(
                painter = painterResource(id = R.drawable.ic_search),
                contentDescription = "search"
            )
        },
        colors = TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            backgroundColor = Color.LightGray,
        )
    )
Run Code Online (Sandbox Code Playgroud)

图片: 在此处输入图片说明

或者,如果您想根据您的 UI/UX 自定义组件,请使用BasicTextField

@Composable
fun ToolbarComponent() {
    var searchText by remember { mutableStateOf("") }
    Row(
        modifier = Modifier
            .padding(16.dp)
            .fillMaxWidth(), verticalAlignment = Alignment.CenterVertically
    ) {

        Icon(
            painter = painterResource(id = R.drawable.ic_search),
            contentDescription = "search",
            modifier = Modifier.size(20.dp),
            tint = iconTintColor
        )

        Spacer(modifier = Modifier.size(16.dp))

        BasicTextField(
            value = searchText,
            onValueChange = { searchText = it },
            modifier = Modifier
                .background(shape = RoundedCornerShape(10.dp), color = Color.LightGray)
                .fillMaxWidth()
                .padding(16.dp),
            decorationBox = {
                Text(text = "Search")
            }
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Gab*_*tti 6

首先,1.2.0-alpha04您可以使用TextFieldDecorationBox和 一起BasicTextField构建基于 Material Design 文本字段的自定义文本字段。

在您的情况下,您可以应用indicatorLine修饰符来定义focusedIndicatorLineThicknessunfocusedIndicatorLineThickness参数:

var text by remember { mutableStateOf("") }
val singleLine = true
val enabled = true
val interactionSource = remember { MutableInteractionSource() }
BasicTextField(
    value = text,
    onValueChange = { text = it },
    modifier = Modifier
        .indicatorLine(enabled, false,
            interactionSource,
            TextFieldDefaults.textFieldColors(),
            focusedIndicatorLineThickness = 0.dp,
            unfocusedIndicatorLineThickness = 0.dp
        )
        .background(
            TextFieldDefaults.textFieldColors().backgroundColor(enabled).value,
            TextFieldDefaults.TextFieldShape
        )
        .width(TextFieldDefaults.MinWidth),
    singleLine = singleLine,
    interactionSource = interactionSource
) { innerTextField ->
    TextFieldDecorationBox(
        value = text,
        innerTextField = innerTextField,
        enabled = enabled,
        singleLine = singleLine,
        visualTransformation = VisualTransformation.None,
        interactionSource = interactionSource,
        label = { Text("Label") }
    )
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

否则,您可以使用TextField定义这些属性:

  • focusedIndicatorColor
  • unfocusedIndicatorColor
  • disabledIndicatorColor

就像是:

    TextField(
        ....
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = .....,
            focusedIndicatorColor =  Transparent,
            unfocusedIndicatorColor = Transparent)
    )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在此输入图像描述


Mut*_*ran 1

如果你使用TextFieldin 那你可以activeColorColor.Transparent

注意:activeColor不仅适用于指示器,还适用于标签底部指示器和光标

前任:

    var text: String by mutableStateOf("")
    TextField(value = text, onValueChange = {
        text = it
    }, activeColor = Color.Transparent)
Run Code Online (Sandbox Code Playgroud)

根据文件,activeColor

activeColor 当文本字段处于焦点时标签、底部指示器和光标的颜色

如果你想使用自己的,你可以尝试BaseTextField