如何确定我的TextField失去焦点后焦点去了哪里 (Android Jetpack Compose)

4
当我的TextField失去焦点时,我该如何确定焦点去了哪里?
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)
    )
}

1
这没关系。我的TextField1失去了焦点,如果焦点转移到其他UI元素,我必须做一些处理,比如TextField2。 - Solvek
2个回答

4

我找到了一个解决方案,但它不是完美的。至少它能够工作。 当input2聚焦时,我设置一个标志,然后如果input1失去焦点,我会等待50ms,如果isInput2Focused标志没有被触发,那么就有一些其他的UI元素被聚焦了。

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
        }
    )
}

0
我已经找到了一个解决这个问题的方法。虽然它看起来不是很好,但是它确实起作用。 我只是使用了两个变量来确定文本框何时失去焦点。
//two types of validattion is there
val isPasswordError = validationErrors.isNotEmpty() && password.isNotEmpty()

//weather is focus or not
var passwordDoneClick by remember { mutableStateOf(false) }

// wather focus gone
var ispasswordFocusedGone by remember { mutableStateOf(false) }

.onFocusChanged {

                            if (it.isFocused){
                                passwordDoneClick = true
                                ispasswordFocusedGone = false
                            }
                            
                            if (passwordDoneClick && !it.isFocused){
                                ispasswordFocusedGone =true
                            }
}
//error to show in ui

            if (isPasswordError && ispasswordFocusedGone ) {
                Text(
                    text = "Password is invalid",
                    style = MaterialTheme.typography.bodyMedium,
                    color = Color.Red
                )
                Spacer(modifier = modifier.height(8.dp))
            } else {
                Spacer(modifier = modifier.height(8.dp))
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接