目标
- 了解常用对话框的类型
- 掌握对话框的一般实现
- 掌握单选(多选)按钮对话框获取选项的方法
- 学会使用对话框来确认程序的退出。
一、消息提示对话框
- 创建AlertDialog.Builder对象
- 设置图标、标题、内容、按钮等区域
- 调用create()方法创建
- 调用show()方法显示对话框
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int position, long l) {
//Toast.makeText(this, "长按"+position, Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.laker);
builder.setTitle("消息提示");
builder.setMessage("这是一个简单的消息提示");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_SHORT).show();
}
});
builder.create().show();
return true;
}
二、确认对话框
- 不调用setMessage()方法设置内容
- 调用setNegativeButton()方法添加取消按钮
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int position, long l) {
//Toast.makeText(this, "长按"+position, Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.laker);
builder.setTitle("消息提示");
// builder.setMessage("这是一个简单的消息提示");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.setNegativeButton("取消", null);
builder.create().show();
return true;
}
三、列表对话框
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int position, long l) {
//Toast.makeText(this, "长按"+position, Toast.LENGTH_SHORT).show();
final String[] arr = {"初始android", "开发环境搭建", "基础控件", "UI控件"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.laker);
builder.setTitle("消息提示");
builder.setItems(arr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.create().show();
return true;
}
四、单选对话框
- 准备一个数组资源
- 调用setSingleChoiceItems()设置单选列表项
int selected = 0;
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int position, long l) {
//Toast.makeText(this, "长按"+position, Toast.LENGTH_SHORT).show();
final String[] arr = {"熊猫", "大象", "长颈鹿", "蜘蛛侠"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.rockets);
builder.setTitle("消息提示");
builder.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
selected = i;
Toast.makeText(MainActivity.this, "用户选择了"+arr[selected], Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
}); // 0表示默认第一个选项
builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.create().show();
return true;
}
五、多选对话框
- 准备一个数组资源
- 调用setMultiChoiceItems()设置多选列表项
六、实现程序退出确认框
- 重写onBackPress方法
- 去除原有的父类方法
- 编写确认对话框
- 在确定事件中国调用finish来结束程序
// 退出
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(R.drawable.rockets)
.setTitle("确认对话框")
.setMessage("确定要退出程序?")
.setNegativeButton("取消", null)
.setPositiveButton("残忍离开", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
// System.exit(0);
}
}).show();
}