/**
* 普通
*/
private void dialogNormal() {
DialogInterface.OnClickListener dialogOnclicListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case Dialog.BUTTON_POSITIVE:
Toast.makeText(MainActivity.this, "确认",
Toast.LENGTH_SHORT).show();
break;
case Dialog.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.this, "取消",
Toast.LENGTH_SHORT).show();
break;
case Dialog.BUTTON_NEUTRAL:
Toast.makeText(MainActivity.this, "忽略",
Toast.LENGTH_SHORT).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("普通对话框");
builder.setMessage("是否确认退出?");
builder.setPositiveButton("确认", dialogOnclicListener);
builder.setNegativeButton("取消", dialogOnclicListener);
builder.setNeutralButton("忽略", dialogOnclicListener);
builder.create().show();
}
/**
* 列表
*/
private void dialogList() {
final String items[] = {"1", "2", "3", "4"};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("列表");
// builder.setMessage("是否确认退出?"); //设置内容
builder.setIcon(R.mipmap.ic_launcher);
// 设置列表显示,注意设置了列表显示就不要设置builder.setMessage()了,否则列表不起作用。
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, items[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
/**
* 单选
*/
private void dialogChoice() {
final String items[] = {"男", "女", "其他"};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("单选");
builder.setIcon(R.mipmap.ic_launcher);
builder.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, items[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
}
});
builder.create().show();
}
/**
* 多选
*/
private void dialogMoreChoice() {
final String items[] = {"JAVA", "C++", "JavaScript", "MySQL"};
final boolean selected[] = {true, false, true, false};
AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("多选");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMultiChoiceItems(items, selected,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(MainActivity.this,
items[which] + isChecked, Toast.LENGTH_SHORT)
.show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Toast.makeText(MainActivity.this, "确定", Toast.LENGTH_SHORT)
.show();
// android会自动根据你选择的改变selected数组的值。
// for (int i = 0; i < selected.length; i++) {
// Log.e("hongliang", "" + selected[i]);
// }
}
});
builder.create().show();
}
/**
* 可输入的对框框
*/
private void dialogEditText() {
final EditText editText = new EditText(this);
final AlertDialog.Builder builder = new AlertDialog.Builder(this,3);
builder.setTitle("可编辑");
builder.setIcon(R.mipmap.ic_launcher);
builder.setView(editText);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, editText.getText().toString() + "", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.create().show();
}
}
AlertDialog 普通 列表 单选 多选 可输入
最新推荐文章于 2022-11-14 17:03:19 发布