本文将介绍常用AlertDialog Api
及其常用AlertDialog
示例
常用API
常用方法
方法定义 | 方法说明 |
---|---|
setIcon(int iconId) | 设置对话框的图标 |
setCancelable(boolean cancelable) | 设置对话框是否可取消 |
setTitle(CharSequence title) | 设置对话框的标题 |
setMessage(CharSequence message) | 设置对话框主体内容 |
setAdapter(ListAdapter adapter, OnClickListener listener) | 设置一个adapter |
setSingleChoiceItems() | 设置单选 |
setMultiChoiceItems() |
设置多选 |
设置宽满屏
Window dialogWindow = alert.getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes();
p.width = WindowManager.LayoutParams.MATCH_PARENT;
dialogWindow.setAttributes(p);
AlertDialog设置宽满屏时显示却有margin属性
alert.getWindow().setBackgroundDrawable(null);
设置居中
alert.getWindow().setGravity(Gravity.CENTER);
Alterdialog点击空白处不消失的方法
alert.setCanceledOnTouchOutside(false);
示例
默认普通对话框
AlertDialog dialog = new AlertDialog
.Builder(this)
.setTitle("普通对话框")
.setIcon(R.mipmap.ic_launcher)
.setNegativeButton("取消", null)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "确定", Toast.LENGTH_SHORT).show();
}
}).setNeutralButton("默认", null)
.setMessage("确认删除?").create();
dialog.show();
显示Item对话框
final CharSequence[] items = {"北京", "上海", "广州"};
AlertDialog dialog = new AlertDialog
.Builder(this)
.setTitle("显示Item对话框")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
单选列表对话框 setSingleChoiceItems
final CharSequence[] items = { "北京", "上海", "广州" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择以下城市");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CharSequence sequence = items[which];
Toast.makeText(getApplicationContext(), "select " + sequence, Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
多选对话框setMultiChoiceItems
final CharSequence[] items = {"北京", "上海", "广州"};
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("多选对话框").setIcon(android.R.drawable.sym_def_app_icon)
.setNegativeButton("取消", null).setPositiveButton("确定", null)
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
自定义Adapter
final String[] items = {"北京", "上海", "广州"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
AlertDialog dialog = new AlertDialog.Builder(this).setTitle("自定义Adapter").setIcon(android.R.drawable.sym_def_app_icon)
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
自定义View
String name = "自定义View";
View view = LayoutInflater.from(this).inflate(android.R.layout.simple_list_item_1, null);
((TextView)view.findViewById(android.R.id.text1)).setText(name);
AlertDialog dialog = new AlertDialog
.Builder(this)
.setTitle(name)
.setIcon(android.R.drawable.sym_def_app_icon)
.setView(view)
.create();
dialog.show();
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。