gpt4 book ai didi

Android 撰写文本字段 : How to set exact 3 lines

转载 作者:行者123 更新时间:2023-12-04 23:43:52 24 4
gpt4 key购买 nike

我要创建TextField精确的 3 行:
destination
即使在此 TextField 中没有任何文本,我也希望看到 3 行,即我需要 EditText.lines 的直接等价物在经典的 xml 布局中。
我不工作的代码是:

OutlinedTextField(
value = currentText,
onValueChange = { currentText = it },
label = { Text ("Label") },
maxLines = 3,
modifier = Modifier.fillMaxWidth().wrapContentHeight().padding(16.dp),
singleLine = false
)
你可以帮帮我吗?

最佳答案

有一个feature request对于此功能,我建议您为它加星,并可能对其发表评论,因为它已经有一段时间没有更新了。
在此之前,您可以使用此 hack。我用额外的行渲染一个不可见的文本字段,使其占据正确的大小,然后将该大小应用于真实的文本字段。我也通过modifiertextStyle作为 remember 的键对于 heightUpdateNeeded因此,如果您更改它们,将重新计算高度。如果您传递的任何其他参数可能会改变 View 的大小,您也应该将它们传递给记住。

@Composable
fun MinLinesOutlinedTextField(
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.Default,
singleLine: Boolean = false,
minLines: Int,
maxLines: Int = Int.MAX_VALUE,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
shape: Shape = MaterialTheme.shapes.small,
colors: TextFieldColors = TextFieldDefaults.outlinedTextFieldColors()
) {
val heightState = remember { mutableStateOf<Int?>(null) }
var heightUpdateNeeded by remember(modifier, textStyle) { mutableStateOf(true) }
val height = with(LocalDensity.current) {
heightState.value?.toDp()
} // to use if nullable unwrapping
Box(modifier.height(IntrinsicSize.Min).width(IntrinsicSize.Min)) {
if (heightUpdateNeeded) {
OutlinedTextField(
value = value + "\n".repeat(minLines),
onValueChange = onValueChange,
enabled = enabled,
readOnly = readOnly,
textStyle = textStyle,
label = label,
placeholder = placeholder,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
isError = isError,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
singleLine = singleLine,
maxLines = maxLines,
interactionSource = interactionSource,
shape = shape,
colors = colors,
modifier = Modifier
.fillMaxSize()
.alpha(0f)
.onSizeChanged {
heightUpdateNeeded = false
println("onSizeChanged $it")
heightState.value = it.height
}
)
}
if (height != null) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
enabled = enabled,
readOnly = readOnly,
textStyle = textStyle,
label = label,
placeholder = placeholder,
leadingIcon = leadingIcon,
trailingIcon = trailingIcon,
isError = isError,
visualTransformation = visualTransformation,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
singleLine = singleLine,
maxLines = maxLines,
interactionSource = interactionSource,
shape = shape,
colors = colors,
modifier = Modifier
.fillMaxWidth()
.height(height)
)
}
}
}

关于Android 撰写文本字段 : How to set exact 3 lines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69040571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com