Android Progressbar Kotlin-显示直到完成一些代码

发布于 2025-01-17 09:21:21 字数 300 浏览 3 评论 0原文

在 Kotlin 中显示覆盖整个应用程序的 Android ProgressBar 直到某些代码完成(例如向数据库添加一些数据)的最简单方法是什么?

可以使用以下代码片段显示进度栏:

val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please Wait")
progressDialog.setMessage("Loading ...")
progressDialog.show()

但是如何轻松使其可见,直到任务完成?

What is the easiest way in Kotlin to show Android ProgressBar covering the whole app untill some code is finished (for example adding some data to database)?

The ProgressBar can be shown using this snippet:

val progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please Wait")
progressDialog.setMessage("Loading ...")
progressDialog.show()

But how can I easily make it visible until the task is done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潇烟暮雨 2025-01-24 09:21:21

好吧,我明白了!

首先,在任何类的顶部以这种方式声明 ProgressBar:

lateinit var progressDialog: ProgressDialog

然后,以这种方式启动它:

progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please Wait")
progressDialog.setMessage("Loading ...")
progressDialog.setCancelable(false) // blocks UI interaction 
progressDialog.show()

上面的代码将阻塞 UI,直到代码完成。完成后,只需隐藏进度条即可:

progressDialog.hide()

简单、整洁、精美!享受! :-)

Ok, I figured it out!

First of all, declare the ProgressBar this way at the top of any class:

lateinit var progressDialog: ProgressDialog

Then, start it this way:

progressDialog = ProgressDialog(this)
progressDialog.setTitle("Please Wait")
progressDialog.setMessage("Loading ...")
progressDialog.setCancelable(false) // blocks UI interaction 
progressDialog.show()

The above code will block UI until your code completes. When it finishes, just hide the progressBar:

progressDialog.hide()

Simple, neat and fine! Enjoy! :-)

一指流沙 2025-01-24 09:21:21

如果您使用jetpack compose;

class MainActivity : ComponentActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            yourTheme{
                MainScreenView()
            }
        }
    }
@Composable
fun MainScreenView(){

    var isLoading by remember {
        mutableStateOf(yourViewModel.isLoading.value)
    }

    if(isLoading){
      //Loading View
    }else{
      //Your Actual View
    }
}
class YourViewModel (
    private val useCases: UseCases
): ViewModel() {

   var isLoading = mutableStateOf(false)
        private set

   fun exampleAddDataToDatabase(data: Data){
        viewModelScope.launch {
            useCases.addDataToDatabase(data).collect{ response ->
                when(response){
                    is Response.Loading -> {isLoading.value = true}
                    is Response.Success -> {isLoading.value = false}
                    is Response.Error -> {}
                }
            }
        }
    }
}

if you are use jetpack compose;

class MainActivity : ComponentActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            yourTheme{
                MainScreenView()
            }
        }
    }
@Composable
fun MainScreenView(){

    var isLoading by remember {
        mutableStateOf(yourViewModel.isLoading.value)
    }

    if(isLoading){
      //Loading View
    }else{
      //Your Actual View
    }
}
class YourViewModel (
    private val useCases: UseCases
): ViewModel() {

   var isLoading = mutableStateOf(false)
        private set

   fun exampleAddDataToDatabase(data: Data){
        viewModelScope.launch {
            useCases.addDataToDatabase(data).collect{ response ->
                when(response){
                    is Response.Loading -> {isLoading.value = true}
                    is Response.Success -> {isLoading.value = false}
                    is Response.Error -> {}
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文