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

标签 android android-textinputlayout android-jetpack-compose android-compose-textfield

我想删除 TextField 的紫色线/指示符(见下图)。
这是可能的还是我应该创建自己的自定义 TextField 来实现这一点?
enter image description here

最佳答案

这已在最近的 Jetpack Compose UI Beta 版本中进行了更改 1.0.0-beta01 现在你可以通过
文本字段默认值 与所需的颜色。

 colors = TextFieldDefaults.textFieldColors(
            focusedIndicatorColor = Color.Transparent,
            disabledIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            backgroundColor = Color.LightGray,
        )
例子
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,
        )
    )
图片:
enter image description here
或者如果您想根据您的 UI/UX 自定义组件,请使用 基本文本字段
@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")
            }
        )
    }
}
enter image description here

关于android - 如何在 Androidx Compose Material 中删除 TextField 的指示线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64363502/

相关文章:

java - AutoCompleteTextView Item 选择触发事件

android - Jetpack Compose ClipToPadding

kotlin - 无法使 ExposedDropdownMenu 与 OutlinedTextField 的宽度相同

android - Jetpack Compose 将文本内容对齐到脚手架的中心

android - hashmap 在 simpleadapter 中工作但在自定义数组适配器中不起作用

android - 在 Android 中访问安全元素

java - TextInputLayout "setError(null)"在 "setErrorEnabled(false)"之前有必要吗?

android - Android 中的 InputTextLayout setError() 方法不起作用

android - Chrome 32 上的 native USB 调试未检测到设备

android - 如何检查应用程序是否可用于新更新? : android