如何确定 TextField 失去焦点后焦点的位置(Android Jetpack Compose) [英] How to determine where focus went after my TextField lost focus (Android Jetpack Compose)
本文介绍了如何确定 TextField 失去焦点后焦点的位置(Android Jetpack Compose)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我的 TextField
失去焦点时,我如何确定焦点去了哪里?
When my TextField
lost focus how can I determine where focus went?
var input1 by remember { mutableStateOf("") }
var input2 by remember { mutableStateOf("") }
Column{
TextField(
value = input1,
onValueChange = {input1=it},
Modifier.width(150.dp).onFocusChanged { state ->
if (state == FocusState.Inactive){
// Here I know that Input1 lost focus but how can I determine for sure the focus went to other then Input2 UI element?
if (focusWentNotToInput2){
// Do semething
}
}
}
)
TextField(
value = input2,
onValueChange = {input2=it},
Modifier.width(150.dp)
)
}
推荐答案
我找到了解决方案,但不是 100% 喜欢它.至少它有效.如果 input2 获得焦点,我正在设置一个标志,如果 input1 失去焦点,我等待 50 毫秒,如果 isInput2Focused
标志没有被触发,那么其他一些 ui 元素被聚焦
I found a solution for this but not like it 100%. At least it works.
I am setting a flag if input2 is focused then if input1 loses focus I wait for 50ms and if isInput2Focused
flag is not triggered then some other ui element is focused
var isInput2Focused by remember { mutableStateOf(false)}
var input1 by remember { mutableStateOf("") }
var input2 by remember { mutableStateOf("") }
val coroutineScope = rememberCoroutineScope()
Column{
TextField(
value = input1,
onValueChange = {input1=it},
Modifier.width(150.dp).onFocusChanged { state ->
if (state.isFocused) isInput2Focused = false
else{
coroutineScope.launch {
delay(50)
if (!isInput2Focused){
// Do semething
}
}
}
}
)
TextField(
value = input2,
onValueChange = {input2=it},
Modifier.width(150.dp).onFocusChanged { state ->
if (state.isFocused) isInput2Focused = true
}
)
}
这篇关于如何确定 TextField 失去焦点后焦点的位置(Android Jetpack Compose)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文