AlertDialog 创建简单的 弹出对话框

效果图:

imageimage

一个 java文件,一个 xml布局文件 只有一个button

java文件代码:

public class Main extends Activity {
 
    private static final String tag = "==CrazyIt.org==";
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Builder b = new AlertDialog.Builder(this);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new OnClickListener() {
 
            public void onClick(View v) {
                // TODO Auto-generated method stub
                b.setIcon(R.drawable.z1);
                b.setTitle("标题");
                b.setMessage("进入了对话框!");
                b.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(Main.this, "点击确定按钮",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                b.setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
 
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(Main.this, "点击取消按钮",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                b.setNeutralButton("aaa",
                        new DialogInterface.OnClickListener() {
 
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                Toast.makeText(Main.this, "点击。。。按钮",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                b.create().show();
            }
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
posted @ 2012-11-16 15:14  高捍得  阅读(253)  评论(0编辑  收藏  举报