android - Jetpack Compose 中的密集文本字段

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

在旧的 View 系统中,我们可以制作 TextInputLayout密集使用风格Widget.MaterialComponents.TextInputLayout.FilledBox.Dense .
如何创建 TextField 的密集变体在撰写?

最佳答案

1.2.0-alpha04 开头您可以使用 TextFieldDecorationBox 连同 BasicTextField基于 Material Design 文本字段构建自定义文本字段。
要创建密集的文本字段,请使用 contentPadding 参数以减少输入字段周围的填充。
就像是:

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())
        .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") },
        contentPadding = TextFieldDefaults.textFieldWithLabelPadding(
            start = 4.dp,
            end = 4.dp,
            bottom = 4.dp // make it dense
        )
    )
}
enter image description here

关于android - Jetpack Compose 中的密集文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68501865/

相关文章:

android - 当 ReceiverRestrictedContext 无法转换为 MyService 时,BroadcastReceiver 如何与 Service 通信?

java - InflateException 使用滑行加载图像时发生

android - Jetpack Compose 可以从任何线程绘制/更新 UI 吗?

android - 在 Jetpack Compose 中更改列中子项的对齐方式

android - 如何使布局组件在android中响应?

android - 将字符串中的字符替换为Image,然后设置为Textview

android - 带有 SwipeToDismiss 的 LazyColumn

android - Jetpack compose mutableStateOf list 在更改列表项类中的属性值时不会触发重新组合

android - 如何减少 androidx.compose.material3.OutlinedTextField 的高度