美文网首页
Compose中,TextField设置高度后,内容被挤压遮挡显

Compose中,TextField设置高度后,内容被挤压遮挡显

作者: littlefogcat | 来源:发表于2023-03-11 01:55 被阅读0次

问题描述

我定义了一个文字输入框:

        TextField(
            value = username,
            singleLine = true,
            modifier = Modifier.fillMaxWidth().height(43.dp),
            colors = TextFieldDefaults.textFieldColors(
                unfocusedIndicatorColor = Color(0xffdddddd),
                backgroundColor = Color.White
            ),
            onValueChange = { username = it }
        )

当这个输入框的高度小于56dp时,就会出现文字显示不完整的问题,如图:


显示不完整

出现这个情况的原因是因为 TextField 中包含了一个 contentPadding,设置了内容离边界的距离。

解决方案

我找遍没有找到简单的解决方法,所以只能自定义了一个控件 EditText,代码是复制的 TextField 的:

package tech.xiaoniu.tp2.widget

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.shape.ZeroCornerSize
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.material.TextFieldDefaults.indicatorLine
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp


@Composable
fun EditText(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = LocalTextStyle.current,
    label: @Composable (() -> Unit)? = null,
    placeholder: @Composable (() -> Unit)? = null,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    isError: Boolean = false,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions(),
    singleLine: Boolean = false,
    maxLines: Int = Int.MAX_VALUE,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    shape: Shape =
        MaterialTheme.shapes.small.copy(bottomEnd = ZeroCornerSize, bottomStart = ZeroCornerSize),
    colors: TextFieldColors = TextFieldDefaults.textFieldColors(),
    paddingValues: PaddingValues = PaddingValues(0.dp)
) {
    // If color is not provided via the text style, use content color as a default
    val textColor = textStyle.color.takeOrElse {
        colors.textColor(enabled).value
    }
    val mergedTextStyle = textStyle.merge(TextStyle(color = textColor))

    @OptIn(ExperimentalMaterialApi::class)
    BasicTextField(
        value = value,
        modifier = modifier
            .background(colors.backgroundColor(enabled).value, shape)
            .indicatorLine(enabled, isError, interactionSource, colors)
            .defaultMinSize(
                minWidth = TextFieldDefaults.MinWidth,
                minHeight = TextFieldDefaults.MinHeight
            ),
        onValueChange = onValueChange,
        enabled = enabled,
        readOnly = readOnly,
        textStyle = mergedTextStyle,
        cursorBrush = SolidColor(colors.cursorColor(isError).value),
        visualTransformation = visualTransformation,
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions,
        interactionSource = interactionSource,
        singleLine = singleLine,
        maxLines = maxLines,
        decorationBox = @Composable { innerTextField ->
            // places leading icon, text field with label and placeholder, trailing icon
            TextFieldDefaults.TextFieldDecorationBox(
                value = value,
                visualTransformation = visualTransformation,
                innerTextField = innerTextField,
                placeholder = placeholder,
                label = label,
                leadingIcon = leadingIcon,
                trailingIcon = trailingIcon,
                singleLine = singleLine,
                enabled = enabled,
                isError = isError,
                interactionSource = interactionSource,
                colors = colors,
                contentPadding = paddingValues
            )
        }
    )
}

用法同 TextField 一样,只不过可以传入一个 contentPadding 参数来控制内容离边框的距离。如:

        // 用户名输入框
        EditText(
            value = username,
            singleLine = true,
            modifier = Modifier.fillMaxWidth().height(27.dp),
            colors = TextFieldDefaults.textFieldColors(
                unfocusedIndicatorColor = Color(0xffdddddd),
                backgroundColor = Color.White
            ),
            onValueChange = { username = it },
            contentPadding = PaddingValues(4.dp) // 设置padding
        )

最终效果:


效果

相关文章

网友评论

      本文标题:Compose中,TextField设置高度后,内容被挤压遮挡显

      本文链接:https://www.haomeiwen.com/subject/rttxrdtx.html