gpt4 book ai didi

android - 如何使用箭头键将焦点从撰写按钮移动到撰写文本字段?

转载 作者:行者123 更新时间:2023-12-04 23:48:39 26 4
gpt4 key购买 nike

如何将焦点从撰写按钮转移到撰写 TextField通过使用箭头键?
当我将焦点从撰写按钮移至撰写时 TextField通过使用箭头键(KeyEvent.KEYCODE_DPAD_UP),
该按键的焦点移动两次。
(当我使用翻页键时)
我希望焦点在那个按键上移动一次。
详情请看GIF动画
有没有办法来解决这个问题?
(我在 TextFields 之间移动时使用向上键和向下键
使用 moveFocus focusManager的方法在 onKeyEvent )

@Composable
fun Screen() {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
val focusManager = LocalFocusManager.current

FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)

OutlinedButton(
onClick = {}
) {
Text("Button")
}
}
}

@Composable
private fun FocusMoveTextField(
focusManager: FocusManager
) {
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.onKeyEvent { keyEvent ->
when (keyEvent.nativeKeyEvent.keyCode) {
KEYCODE_DPAD_DOWN -> {
focusManager.moveFocus(FocusDirection.Down)
true
}
KEYCODE_DPAD_UP -> {
focusManager.moveFocus(FocusDirection.Up)
true
}
else -> {
false
}
}
},
value = "",
onValueChange = {},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
)
)
}

最佳答案

测试后我看到 onKeyEventButton 上按一个键后会触发 2 次(ACTION_DOWN 为 1,ACTION_UP 为 1)。也许它会导致按键时焦点移动两次。
那么我认为你可以专注于你的 Button喜欢

val focusRequester = remember { FocusRequester() }
var buttonColor by remember { mutableStateOf(Black) }
OutlinedButton(
onClick = { focusRequester.requestFocus() }, modifier = Modifier
.focusRequester(focusRequester)
.onFocusChanged { buttonColor = if (it.isFocused) Green else Black }
.focusTarget()
.onKeyEvent { keyEvent ->
if (keyEvent.nativeKeyEvent.action == ACTION_UP) {
when (keyEvent.nativeKeyEvent.keyCode) {
KEYCODE_DPAD_DOWN -> {
focusManager.moveFocus(FocusDirection.Down)
true
}
KEYCODE_DPAD_UP -> {
focusManager.moveFocus(FocusDirection.Up)
true
}
else -> {
false
}
}
} else {
true
}
}
) {
Text("Button", color = buttonColor)
}
完整示例
@Composable
fun Screen() {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally
) {
val focusManager = LocalFocusManager.current

FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)

val focusRequester = remember { FocusRequester() }
var buttonColor by remember { mutableStateOf(Black) }
OutlinedButton(
onClick = { focusRequester.requestFocus() }, modifier = Modifier
.focusRequester(focusRequester)
.onFocusChanged { buttonColor = if (it.isFocused) Green else Black }
.focusTarget()
.onKeyEvent { keyEvent ->
if (keyEvent.nativeKeyEvent.action == ACTION_UP) {
when (keyEvent.nativeKeyEvent.keyCode) {
KEYCODE_DPAD_DOWN -> {
focusManager.moveFocus(FocusDirection.Down)
true
}
KEYCODE_DPAD_UP -> {
focusManager.moveFocus(FocusDirection.Up)
true
}
else -> {
false
}
}
} else {
true
}
}
) {
Text("Button", color = buttonColor)
}
FocusMoveTextField(focusManager)
FocusMoveTextField(focusManager)
}
}

@Composable
private fun FocusMoveTextField(
focusManager: FocusManager
) {
OutlinedTextField(
modifier = Modifier
.fillMaxWidth()
.onKeyEvent { keyEvent ->
when (keyEvent.nativeKeyEvent.keyCode) {
KEYCODE_DPAD_DOWN -> {
focusManager.moveFocus(FocusDirection.Down)
true
}
KEYCODE_DPAD_UP -> {
focusManager.moveFocus(FocusDirection.Up)
true
}
else -> {
false
}
}
},
value = "",
onValueChange = {},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number,
imeAction = ImeAction.Done
)
)
}

关于android - 如何使用箭头键将焦点从撰写按钮移动到撰写文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70669774/

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