0

我有这个自定义对话框:

public class MyProgressDialog extends Dialog {

    public MyProgressDialog(Context context) {
        super(context);
    }

    public static MyProgressDialog show(Context context) {
        MyProgressDialog dialog = new MyProgressDialog(context);

        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.progress);
        dialog.getWindow().setBackgroundDrawableResource(
                android.R.color.transparent);
        dialog.show();
        return dialog;
    }

    public static void dismiss(Context context) {
        MyProgressDialog dialog = new MyProgressDialog(context);
        dialog.dismiss();
    }

}

它会显示

    protected void onPreExecute() {
        MyProgressDialog.show(ItemsActivity.this);
        }

但不会解雇:

MyProgressDialog.dismiss(ItemsActivity.this);

有谁知道为什么?

4

1 回答 1

2

您正在创建一个新对话框并关闭它,而不是您在show(). 您可以保留对创建的最新对话框的引用,如果愿意,可以将其关闭。例如:

public static MyProgressDialog show(Context context) {
    MyProgressDialog dialog = new MyProgressDialog(context);

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.progress);
    dialog.getWindow().setBackgroundDrawableResource(
            android.R.color.transparent);
    dialog.show();
    staticVariable = dialog;
    return staticVariable;
}

public static void dismiss(Context context) {
    staticVariable.dismiss();
}

然而,这似乎不是一个理想的设计。

于 2012-10-19T14:16:49.357 回答