【问题标题】:how do I dismiss my progress dialog after the needed function in alert dialog is applied?应用警报对话框中所需的功能后,如何关闭我的进度对话框?
【发布时间】:2016-06-18 08:07:12
【问题描述】:

我的片段中有一个链接,显示“单击以打开对话框”,单击它时会弹出一个警告对话框,显示“您要更改视图吗?”带有是和否符号。如果我单击“否”,则不会发生任何事情,并且对话框会按预期消失。但是,如果我单击是,如何调用函数来刷新我的视图,直到我的视图刷新显示进度对话框?到目前为止,这是我的代码,我不知道在哪里关闭进度对话框或确保刷新视图,有什么线索吗? :

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Are you sure you want to refresh your view?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                //refreshview here ? 

                ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "",
                        "Loading. Please wait...", true);
                progressDialog.show();

                progressDialog.setCancelable(false);
                progressDialog.setCanceledOnTouchOutside(false);
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

【问题讨论】:

  • onClickPositiveButton 中添加dialog.dismiss();

标签: android android-view android-alertdialog android-progressbar android-runonuithread


【解决方案1】:

如果您不在新线程上执行任何操作,则无需处理程序。只需调用如下方法:

private void updateView(ProgressDialog dialog){
   //UPDATE
   if(dialog != null){
     dialog.dismiss();
   } 
}

【讨论】:

    【解决方案2】:

    最好在此处和onPostExecute(...) 中使用AsyncTask 关闭您的Progress Dialog

    看看演示

    1. Official Page
    2. Examples

    一旦您获得数据,请在 onPostExecute(...) 中刷新您的 View

    编辑:

     //Progress Dialog Show logic here
     Runnable r=new Runnable() {
            @Override
            public void run() {
                //Dismiss the Dialog and refresh your Views
            }
        };
    
        new Handler().postDelayed(r,10*1000);
    

    此代码在 10 秒后执行。

    【讨论】:

    • 根据我当前的代码,如果没有 asynctask 就没有更简单的方法吗?
    • @JusticeBauer 是的,另一种方法是使用Handler
    • 你能告诉我如何在我的代码中使用它吗?
    • @JusticeBauer 但是让我澄清一件事是您的新数据来自服务器以刷新视图?
    • 不,不是,我想在本地刷新视图,因为在警报屏幕上单击“是”时再次加载它
    猜你喜欢
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多