我将假设你已知道:

  • Dialog是什么

貌似Android要显示Dialog有两个办法:

  1. Activity类中,创建一个 protected Dialog onCreateDialog(int id) 方法,然后在Activity内任意位置调用 showDialog(DIALOG_PAUSED_ID); 。这里DIALOG_PAUSED_ID是个int
  2. 在任意位置直接就地创建一个Dialog,然后调用其show()方法。

我只成功尝试了方法1,但我不知道这是为什么。两种方法都应该可用。以下是方法1的例子。

List.java 的内容:

public class List extends Activity {
    private static final int DIALOG_ID_DELETE = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        showDialog(DIALOG_ID_DELETE);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_ID_DELETE:
            AlertDialog.Builder builder = new AlertDialog.Builder(List.this);
            builder.setTitle(caption);
            builder.setMessage(R.string.list_alert_Delete_message_row);
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setPositiveButton(android.R.string.yes,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            toaster("delete attempted");
                            fillListApprop(mode);
                        }
                    });
            builder.setNegativeButton(android.R.string.no,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            return builder.create();
        default:
            Toast.makeText(getApplicationContext(),"something went wrong with the dialog", Toast.LENGTH_SHORT).show();
            return null;
        }
    }

}

参考:

别的参考忘了。