在Android应用中显示进度对话框

时间:2012-10-15 15:32:41

标签: android android-activity dialog progressdialog

我想在名为myActivity的活动中使用进度对话框。 我从活动中的方法启动它:

progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...");

但没有出现。为什么呢?

我也试过这一行:

progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...",true);

具有相同的结果。

2 个答案:

答案 0 :(得分:2)

就是这个

//Declare progressDialog before so you can use .hide() later!
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

答案 1 :(得分:0)

请在您的代码中添加可运行线程

示例:https://abhiandroid.com/ui/progressdialog

或查看此示例代码

progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while (progressDialog.getProgress() <= progressDialog.getMax()) {
                Thread.sleep(200);
                handle.sendMessage(handle.obtainMessage());
            if (progressDialog.getProgress() == progressDialog.getMax()) {
                progressDialog.dismiss();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}}).start();