效果:
调用代码:
new AlertDialog.Builder(MainActivity.this) .setIcon(android.R.drawable.ic_dialog_alert) .setTitle("注意:请谨慎选择!选错手机会爆炸!") .setMessage("我是不是最好看的人?") .setPositiveButton(R.string.confirm,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { finish(); } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .create() .show();
调用代码:
//单选对话框 new AlertDialog.Builder(MainActivity.this) .setTitle("选择性别") .setCancelable(false) .setSingleChoiceItems(new CharSequence[]{"男","女"},0,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { MainActivity.this.setTitle("恩恩,选好啦"); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { MainActivity.this.setTitle("取消干嘛"); } }) .create() .show();
调用代码:
String[] addr=new String[]{"北京","上海","天津","广州","重庆"}; new AlertDialog.Builder(MainActivity.this) .setTitle("宝贝儿,选个城市吃火锅~") .setMultiChoiceItems(addr, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface arg0, int arg1, boolean arg2) { } }) .setPositiveButton("确定",new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .setNegativeButton("取消", new OnClickListener(){ @Override public void onClick(DialogInterface arg0, int arg1) { // TODO Auto-generated method stub } }) .create().show();
代码:
//列表对话框
String[] addr=new String[]{"北京","上海","天津","深圳"};
new AlertDialog.Builder(MainActivity.this)
.setTitle("城市列表框")
.setItems(addr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.create().show();
注意:
一些名称,比如按钮名称,title,等要写成XML写到strings.xml文件中,以上仅是Demo.