Android对话框
- 1 普通Dialog
- 2 列表Dialog
- 3 单选Dialog
- 4 多选Dialog
- 5自定义Dialog
普通Dialog:
private void showNormalDialog(){
final AlertDialog.Builder normalDialog =
new AlertDialog.Builder(MainActivity.this);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle("我是一个普通Dialog")
normalDialog.setMessage("你要点击哪一个按钮呢?");
normalDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.setNegativeButton("关闭",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//...To-do
}
});
normalDialog.show();
}
列表Dialog:
private void showListDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
AlertDialog.Builder listDialog =
new AlertDialog.Builder(MainActivity.this);
listDialog.setTitle("我是一个列表Dialog");
listDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
"你点击了" + items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
}
单选Dialog:
int yourChoice;
private void showSingleChoiceDialog(){
final String[] items = { "我是1","我是2","我是3","我是4" };
yourChoice = -1;
AlertDialog.Builder singleChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
singleChoiceDialog.setTitle("我是一个单选Dialog");
singleChoiceDialog.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (yourChoice != -1) {
Toast.makeText(MainActivity.this,
"你选择了" + items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
}
多选Dialog:
ArrayList<Integer> yourChoices = new ArrayList<>();
private void showMultiChoiceDialog() {
final String[] items = { "我是1","我是2","我是3","我是4" };
final boolean initChoiceSets[]={false,false,false,false};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new AlertDialog.Builder(MainActivity.this);
multiChoiceDialog.setTitle("我是一个多选Dialog");
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
yourChoices.add(which);
} else {
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = yourChoices.size();
String str = "";
for (int i = 0; i < size; i++) {
str += items[yourChoices.get(i)] + " ";
}
Toast.makeText(MainActivity.this,
"你选中了" + str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
}
自定义Dialog:
private void showCustomizeDialog() {
AlertDialog.Builder customizeDialog =
new AlertDialog.Builder(MainActivity.this);
final View dialogView = LayoutInflater.from(MainActivity.this)
.inflate(R.layout.dialog_customize,null);
customizeDialog.setTitle("我是一个自定义Dialog");
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.this,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}