【问题标题】:Hide/Show CircularProgressIndicator onclick Jetpack Compose隐藏/显示 CircularProgressIndicator onclick Jetpack Compose
【发布时间】:2021-07-27 15:27:48
【问题描述】:

我想在点击时显示 CircularProgressIndicator 然后隐藏!函数执行时的 CircularProgressIndicator 请参见下面的代码

var loader by remember {mutableStateOf(false)}
if(loader){
   CircularProgressIndicator()
}
Button (onclick {
   loader = !loader // show CircularProgressIndicator
   // Function which is to be executed 
   // After function is executed which usually takes 2 sec then after that CircularProgressIndicator should be hidden
   loader = loader // Not hidding
}){
  Text("Toggle")
}

【问题讨论】:

    标签: android-jetpack-compose


    【解决方案1】:

    基本上,您需要检查在处理结束时是否应该调用firebase = false。我建议你至少将逻辑移到一个单独的函数中,这样我会更难出错,但基本上它应该看起来像这样:

    OutlinedButton(onClick = {
        keyboardController?.hide()
        if (emailState.value.isEmpty() && secureState.value.isEmpty())
            return@OutlinedButton
        firebase = true
        FirebaseAuth.getInstance()
            .createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener { task ->
                val exception = task.exception?.localizedMessage.toString()
                if (task.isSuccessful)
                    FirebaseAuth.getInstance().currentUser?.sendEmailVerification()
                        ?.addOnCompleteListener { task ->
                            firebase = true
                            if (task.isSuccessful)
                                snackbarCoroutineScope.launch {
                                    scaffoldState.snackbarHostState.showSnackbar(
                                        "Check your Email for Verification")
                                }
                        }
                else
                    snackbarCoroutineScope.launch {
                        scaffoldState.snackbarHostState.showSnackbar(exception)
                        firebase = true
                    }
            }
    }) {
        Text(text = "Sign Up")
    }
    Spacer(modifier = Modifier.size(16.dp))
    TextButton(onClick = {
        keyboardController?.hide()
        if (emailState.value.isEmpty())
            return@TextButton
        FirebaseAuth.getInstance().sendPasswordResetEmail(email)
            .addOnCompleteListener { task ->
                val exception = task.exception?.localizedMessage.toString()
                firebase = false
                if (task.isSuccessful)
                    snackbarCoroutineScope.launch {
                        scaffoldState.snackbarHostState.showSnackbar("Check your Email for Password reset")
                        firebase = true
                    }
                else
                    snackbarCoroutineScope.launch {
                        scaffoldState.snackbarHostState.showSnackbar(exception)
                        firebase = true
                    }
            }
    }) {
        Text(text = "Forgot password?")
    }
    

    【讨论】:

    • @Unknown 您的处理功能究竟是如何工作的?如果是协程,loader = !loader 应该在协程内,如果它只是一个阻塞函数,它应该阻塞你的 UI,你根本看不到进度指示器。..
    • 是的,我正在使用协程
    • @Unknown 我已经更新了我的答案,如果这仍然没有帮助,请添加更多代码:处理函数的样子(不需要处理部分,只需要语义)以及你如何执行它
    • 谢谢,是的,我应该将逻辑移动到单独的函数中,感谢 su
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 2015-05-03
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    相关资源
    最近更新 更多