Problem using ProgressDialog with onCreateDialog / onPrepareDialog

[亡魂溺海] 提交于 2019-12-10 17:38:06

问题


I'm using the following code to create a ProgressDialog (inside my Activity):

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_LOOKUP:
            return new ProgressDialog(this, ProgressDialog.STYLE_SPINNER);
    }
    return null;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    switch (id) {
        case DIALOG_LOOKUP:
            dialog.setCancelable(true);
            dialog.setTitle(R.string.dialogLookup_title);
            ((ProgressDialog)dialog).setMessage(getResources().getString(R.string.dialogLookup_message));
            dialog.setOnCancelListener(new OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    Toast.makeText(MyActivity.this, "canceled", Toast.LENGTH_SHORT).show();
                }
            });
            break;
    }
}

The problem is that it isn't actually setting the title and is putting it in some weird double-box.

It's giving me this:

but I'm expecting something more like this:

Any ideas?


回答1:


I just tried your sample and it seems changing from ProgressDialog.STYLE_SPINNER to ProgressDialog.STYLE_HORIZONTAL fixed the weird double-box problem.

And it also displays the title and text.

Edit:

You are passing ProgressDialog.STYLE_SPINNER in the ProgressDialog constructor.

From the doc, the 2nd argument is a theme id.

You will have to create a ProgressDialog object and use the setProgressStyle to ProgressDialog.STYLE_SPINNER

case DIALOG_LOOKUP:
     ProgressDialog pdlg = new ProgressDialog(this);
     pdlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
     return pdlg;


来源:https://stackoverflow.com/questions/2195497/problem-using-progressdialog-with-oncreatedialog-onpreparedialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!