如何测量textField的高度
how to measure the height of textField
代码:
val focusManager = LocalFocusManager.current
var code by remember { mutableStateOf("") }
var money by remember { mutableStateOf("") }
Column() {
Row() {
TextField(
modifier = Modifier.wrapContentSize(),
value = code,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
focusManager.clearFocus()
}),
singleLine = true,
label = { Text("Code") },
onValueChange = { pscCode ->
code = pscCode.filter { it.isDigit() }.take(16)
})
Button(modifier = Modifier.fillMaxWidth(),
onClick = {…}
)
{
Text("Save data")
}
}
Row() {
TextField(
modifier = Modifier.wrapContentSize(),
value = money,
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
focusManager.clearFocus()
}),
label = { Text("Money amount") },
onValueChange = { moneyAmount ->
money = moneyAmount
.trim()
.replace(",", ".")
.take(5)
})
Button(modifier = Modifier.fillMaxWidth(),
onClick = {…}
) {
Text("Clean data")
}
}
}
我一直在尝试使按钮的大小与 textFields 的大小相匹配,但我没有找到方法,我尝试测量 textBoxes 的高度(没有结果),有没有更好的(并且有效) ) 填补这些空白的方法?
提前致谢:)
菲利普的回答也应该这样做。
另一种解决方法
var textFieldHeight: Dp
BoxWithConstraints(Modifier.wrapContentSize()) {
TextField()
textFieldHeight = maxHeight
}
Row(Modifier.height(IntrinsicSize.Min)) {
TextField( /* your code */)
Button(
modifier = Modifier.fillMaxHeight(),
onClick = {}
)
{
Text("Save")
}
}
如文档中所述:
The Row composable's minIntrinsicHeight
will be the maximum minIntrinsicHeight
of its children. The Button
element's minIntrinsicHeight
is 0
as it doesn't occupy space if no constraints are given; the TextField
minIntrinsicHeight
will be that of the text given a specific width. Therefore, the Row
element's height constraint will be the max minIntrinsicHeight
of the TextField
. Button
will then expand its height to the height constraint given by the Row
.
代码:
val focusManager = LocalFocusManager.current
var code by remember { mutableStateOf("") }
var money by remember { mutableStateOf("") }
Column() {
Row() {
TextField(
modifier = Modifier.wrapContentSize(),
value = code,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
focusManager.clearFocus()
}),
singleLine = true,
label = { Text("Code") },
onValueChange = { pscCode ->
code = pscCode.filter { it.isDigit() }.take(16)
})
Button(modifier = Modifier.fillMaxWidth(),
onClick = {…}
)
{
Text("Save data")
}
}
Row() {
TextField(
modifier = Modifier.wrapContentSize(),
value = money,
singleLine = true,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
focusManager.clearFocus()
}),
label = { Text("Money amount") },
onValueChange = { moneyAmount ->
money = moneyAmount
.trim()
.replace(",", ".")
.take(5)
})
Button(modifier = Modifier.fillMaxWidth(),
onClick = {…}
) {
Text("Clean data")
}
}
}
我一直在尝试使按钮的大小与 textFields 的大小相匹配,但我没有找到方法,我尝试测量 textBoxes 的高度(没有结果),有没有更好的(并且有效) ) 填补这些空白的方法? 提前致谢:)
菲利普的回答也应该这样做。
另一种解决方法
var textFieldHeight: Dp
BoxWithConstraints(Modifier.wrapContentSize()) {
TextField()
textFieldHeight = maxHeight
}
Row(Modifier.height(IntrinsicSize.Min)) {
TextField( /* your code */)
Button(
modifier = Modifier.fillMaxHeight(),
onClick = {}
)
{
Text("Save")
}
}
如文档中所述:
The Row composable's
minIntrinsicHeight
will be the maximumminIntrinsicHeight
of its children. TheButton
element'sminIntrinsicHeight
is0
as it doesn't occupy space if no constraints are given; theTextField
minIntrinsicHeight
will be that of the text given a specific width. Therefore, theRow
element's height constraint will be the maxminIntrinsicHeight
of theTextField
.Button
will then expand its height to the height constraint given by theRow
.