[英]Resizeable BasicTextField in Jetpack Compose
Is there a way to produce a resizeable BasicTextField in Jetpack Compose, so that its width would wrap text size when a user types in or deletes characters?有没有办法在 Jetpack Compose 中生成可调整大小的 BasicTextField,以便在用户输入或删除字符时它的宽度会换行文本大小? They've solved a similar problem for flutter, but I didn't find out how to solve this for Compose.
他们已经解决了 flutter 的类似问题,但我没有找到如何为 Compose 解决这个问题。 Flutter - how to make TextField width fit its text ("wrap content")
Flutter - 如何使 TextField 宽度适合其文本(“包装内容”)
var text: String by rememberSaveable { mutableStateOf("") }
BasicTextField(
value = text,
onValueChange = {
text = it
},
modifier = Modifier.wrapContentWidth()
)
unfortunately, wrapContentWidth()
doesn't work here.不幸的是,
wrapContentWidth()
在这里不起作用。
Well, looks like width(IntrinsicSize.Min)
solves this problem:好吧,看起来像
width(IntrinsicSize.Min)
解决了这个问题:
var text: String by rememberSaveable { mutableStateOf("") }
BasicTextField(
value = text,
onValueChange = {
text = it
},
modifier = Modifier.width(IntrinsicSize.Min)
)
We can specify the width range ( min and max width that the textField can span )我们可以指定宽度范围( textField 可以跨越的最小和最大宽度)
for width:-宽度:-
modifier = Modifier.widthIn(1.dp, Dp.Infinity) // specified the min width as 1.dp
for height:-身高:-
modifier = Modifier.heightIn(1.dp, Dp.Infinity)
The composable code:-可组合代码:-
Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
OutlinedTextField(
value = text,
onValueChange = {
text = it
},
modifier = Modifier.widthIn(1.dp, Dp.Infinity)
)
}
The TextField
would grow when we type more till Dp.Infinity
.当我们输入更多直到
Dp.Infinity
时, TextField
会增长。 ( used OutlinedTextField
for demo but we can use BasicTextField
) (使用
OutlinedTextField
进行演示,但我们可以使用BasicTextField
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.