gpt4 book ai didi

android - OutlinedTextField jetpack 在键盘后面组成

转载 作者:行者123 更新时间:2023-12-05 00:02:22 24 4
gpt4 key购买 nike

我使用 Jetpack Compose 来制作应用程序 UI,但它有一些问题:当单击键盘上的下一个按钮时,它会将焦点移动到下一个 TextField (金额),这很好,但问题是 TextField保持在键盘后面,这意味着屏幕动画没有触发:
如何将屏幕移动到新的下一个输入?

@Composable
fun KeyboardSample() {
Scaffold(
modifier = Modifier
.fillMaxSize()
.padding(start = 16.dp, end = 16.dp),

) {

val name = rememberSaveable { mutableStateOf("") }
val updateName = { _name: String ->
name.value = _name
}

val amount = rememberSaveable { mutableStateOf("") }
val updateAmount = { _amount: String ->
amount.value = _amount
}

TextFieldsToExperiment(
name = name.value,
updateName = updateName,
amount = amount.value,
updateAmount = updateAmount
)

}
}

@Composable
fun TextFieldsToExperiment(
name: String,
updateName: (String) -> Unit,
amount: String,
updateAmount: (String) -> Unit
) {
val focusManager = LocalFocusManager.current
val focusRequester = FocusRequester()
Column(
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Spacer(modifier = Modifier.height(390.dp))
OutlinedTextField(
value = name,
onValueChange = updateName,
label = { Text("Name") },
placeholder = { Text(text = "Name") },
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
capitalization = KeyboardCapitalization.Sentences,
autoCorrect = true,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(onNext = {
focusManager.moveFocus(FocusDirection.Down)
}),
modifier = Modifier
.fillMaxWidth()
.padding(top = 6.dp, start = 0.dp, end = 0.dp, bottom = 6.dp),
)
Text(text = "Hello")
Spacer(Modifier.height(8.dp))
OutlinedTextField(
value = amount,
onValueChange = updateAmount,
label = { Text("Amount") },
placeholder = { Text(text = "Amount") },
singleLine = true,
keyboardOptions = KeyboardOptions.Default.copy(
capitalization = KeyboardCapitalization.Sentences,
autoCorrect = true,
keyboardType = KeyboardType.Text,
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(onDone = {
focusManager.clearFocus()
}),
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
.padding(top = 6.dp, start = 0.dp, end = 0.dp, bottom = 6.dp),
)

}
}
另外,我添加了一些图片来更好地说明这个问题:
无键盘用户界面:

以名称为中心

以金额为中心

最佳答案

把你的键盘示例() 中的内容懒人专栏 像这样

@Composable
fun KeyboardSample() {
Scaffold(
modifier = Modifier
.fillMaxSize()
.padding(
start = 16.dp,
end = 16.dp
),

) {

val name = rememberSaveable { mutableStateOf("") }
val updateName = { _name: String ->
name.value = _name
}

val amount = rememberSaveable { mutableStateOf("") }
val updateAmount = { _amount: String ->
amount.value = _amount
}


LazyColumn{
item {
TextFieldsToExperiment(
name = name.value,
updateName = updateName,
amount = amount.value,
updateAmount = updateAmount
)
}
}

}
}
最重要的是将此添加到 list 中的 Activity 中
android:windowSoftInputMode="adjustResize"

关于android - OutlinedTextField jetpack 在键盘后面组成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70126749/

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