第一种:普通dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.follow);
builder.setTitle("普通对话框:提示");
builder.setMessage("是否要删除?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT).show();
}
});
builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
Toast.makeText(MainActivity.this, "忽略", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
第二种:列表diglog
final String[] name = {"土豆","豆角","西红柿"};
AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
listDialog.setIcon(R.drawable.follow)
.setTitle("蔬菜列表")
.setItems(name, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
Toast.makeText(MainActivity.this, name[pI]+"", Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
第三种:单选dialog
final String[] names = {"土豆","豆角","西红柿"};
AlertDialog.Builder singleSelect = new AlertDialog.Builder(this);
singleSelect.setIcon(R.drawable.follow)
.setTitle("蔬菜单选列表")
.setSingleChoiceItems(names, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
Toast.makeText(MainActivity.this, names[pI]+"", Toast.LENGTH_SHORT).show();
}
});
singleSelect.show();
第四种:多选dialog
final String[] food = {"土豆","豆角","西红柿"};
final ArrayList<Integer> integers = new ArrayList<>();
boolean isSelect[] = {false,false,false};
integers.clear();
AlertDialog.Builder moreSelect = new AlertDialog.Builder(this);
moreSelect.setIcon(R.drawable.follow)
.setTitle("蔬菜多选列表")
.setMultiChoiceItems(food, isSelect, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI, boolean pB) {
if(pB){
integers.add(pI);
}else{
integers.remove(pI);
}
}
});
moreSelect.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface pDialogInterface, int pI) {
int size = integers.size();
String str = "";
for (int i = 0; i < size; i++) {
str += food[integers.get(i)]+"\n";
}
Toast.makeText(MainActivity.this, "选中了:"+str, Toast.LENGTH_SHORT).show();
}
});
moreSelect.show();