---
title: "打开程序在activity上出现一个AlertDialog"
author: "liangoogle"
date: "2011-06-08T17:18:10Z"
tags: []
categories: ["移动开发"]
slug: "activity-alertdialog"
draft: false
lastmod: "2011-06-08T17:18:10Z"
id: 1073309
url: "https://liangoogle.iteye.com/blog/1073309"
---

只要把  showDialog(0); 写在oncreat里，这样打开应用就会加载。

public class Test extends Activity {

    /\** Called when the activity is first created. \*/

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        showDialog(0);

        Button button1 = (Button) findViewById(R.id.button1);

        button1.setOnClickListener(new Button.OnClickListener() {

 

            public void onClick(View v) {

                // TODO Auto-generated method stub

                showDialog(0);

            }

        });

 

    }

 

    @Override

    protected Dialog onCreateDialog(int id) {

        switch (id) {

            case 0: {

                Dialog dialog = new AlertDialog.Builder(Test.this).setTitle("Question")

                        .setMessage("Are you sure that you want to quit?") // 设置内容

                        .setPositiveButton("Yes", // 设置确定按钮

                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {

                                        setResult(RESULT\_OK);

                                        finish();

                                    }

                                }).setNegativeButton("No", new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog, int whichButton) {

                            }

                        }).create();// 创建

                return dialog;

            }

            default: {

                return null;

            }

        }

    }

 

 

 

 

 

 

}