publicvoidonClick(View v){
//创建对话框构造器
AlertDialog.Builder b = new Builder(this);
b.setIcon(Android.R.drawable.xxxx);
b.setTitle("abcd");
b.setMessage("message");
// 这里的OnClickListener和Button的不是一个类
b.setPositiveButton("COMFIRM", new OnClickListener(){
publicvoidonClick(DialogInterface i, int which){}
});
b.setNegativeButton("CANCEL",new OnClickListener(){
publicvoidonClick(DialogInterface i,int which){}
});
//Builder对象生成创建后显示出来,create返回的是AlertDialog
b.create().show();
}
单选框
publicvoidclick2(View v){
AlertDialog.Builder b = new Builder(this);
b.setTitle("我是单选框");
final String[] items = new String[]{"1","2"};
// -1就是一个不选
b.setSingleChoiceItems(items, -1, new OnClickListener(){
publicvoidonClick(DialogInterface d, int which){
d.dismiss();//关闭对话框
}
});
b.show(); //不执行Create也可以
}
多选对话框
publicvoidonCreate(){
AlertDialog.Builder b = new Builder(this);
String[] items = new String[]{"1", "2", "3"};
boolean[] checkedItems = newboolean[]{false,false,false}; //记录哪个被选中
b.setMultiChoiceItems(items, checkedItems, new OnMuliChoiceClickListener(){
publicvoidonClick(DialogInterface d, int which, boolean isChecked){
//which是第几个,isChecked表示是否选择
checkItems[which] = isChecked;
}
});
b.setPositiveButton();//和上面的类似,选中后确定,数据就用上面的变量
}