Compose BasicTextField 的问题:

Problems with Compose BasicTextField:

现在我有了一个自定义的撰写输入框。 BasicTextField,但无论如何我都无法将输入字体居中。我只能通过Box.

的偏移属性在x方向偏移

自定义输入框的原因是因为我需要一个固定高度的输入框和字体大小。但是文本框高度不够时无法输入

请帮助我,我更喜欢使用TextField 来编写具有固定高度的输入框。但是在网站上没有找到合适的解决方案。

这是我的自定义代码:

@Composable
fun InputEditText(
    value: String,
    modifier: Modifier,
    onValueChange: (String) -> Unit,
    contentTextStyle: TextStyle,
    hintTextStyle: TextStyle,
    placeHolderString: String = "",
    enabled: Boolean = true,
    readOnly: Boolean = false,
    singleLine: Boolean = false,
    maxLines: Int = Int.MAX_VALUE,
    offsetDp: Dp = 10.dp,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    cursorColor: Color = Color.Black,
) {
    BasicTextField(
        value = value,
        onValueChange = onValueChange,
        modifier = modifier,
        textStyle = contentTextStyle,
        decorationBox = {innerTextField ->
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .offset(x = offsetDp),
                contentAlignment = Alignment.CenterStart,
            ) {
                if (value.isEmpty()) {
                    Text(
                        text = placeHolderString,
                        color = hintTextStyle.color,
                        fontSize = hintTextStyle.fontSize
                    )
                }

                innerTextField()

            }
        },
        enabled = enabled,
        readOnly = readOnly,
        singleLine = singleLine,
        maxLines = maxLines,
        keyboardOptions = keyboardOptions,
        keyboardActions = keyboardActions,
        cursorBrush = SolidColor(cursorColor)
    )
}

您可以使用类似的东西:

BasicTextField(
    textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
    decorationBox = {innerTextField ->
        Box(
            contentAlignment = Alignment.Center 
        ) {
            if (text.isEmpty()) {
                Text(
                    text = "placeHolder",
                )
            }
            innerTextField()
        }
    }
)