简介
此次讲解Android应用中最常见的功能——对话框(Dialog),包括普通对话框、单选对话框以及多选对话框的实现
正文
因为手机没有录屏工具,就没有弄成动态图,直接截图喽
<img src="http://upload-images.jianshu.io/upload_images/2306667-0624966446746f7b.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="100" height="200">
创建对话框构建器对象
AlertDialog.Builder builder = new Builder(this);设置图标、标题和正文
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("史上最帅的男人");
builder.setMessage("徐大爷徐大爷徐大爷徐大爷徐大爷徐大爷");
- 设置确定和取消按钮
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"感谢同意,祝你找到好工作!",Toast.LENGTH_LONG).show();
dialogInterface.dismiss();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"不同意的人工资都特别低!特别低!特别低!",Toast.LENGTH_SHORT).show();
//对话框消失
dialogInterface.dismiss();
}
});
builder.show();
<img src="http://upload-images.jianshu.io/upload_images/2306667-996cb6a59332f66f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="100" height="200">
单选框
核心代码
public void click2(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("你认为自己属于什么性别");
final String[] items = new String[]{
"男",
"女",
"程序员",
"女博士"
};
builder.setSingleChoiceItems(items, -1, new OnClickListener() {
//i:用户所选条目的下标
//dialogInterface:触发这个方法的对话框
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"您选择的是:"+items[i],Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,"卧槽,你是什么物种?",Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
//展现对话框
builder.show();
}
<img src="http://upload-images.jianshu.io/upload_images/2306667-2d6c9d8c3563c3c7.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="100" height="200">
多选框
核心代码
public void click3(View view){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher);
builder.setTitle("请选择你认为最帅的人");
final String[] items=new String[]{
"Trap_余生",
"徐大爷",
"徐大哥",
"晨哥",
"晨爷"
};
final boolean[] checkedItems=new boolean[]{
true,true,true,true,true
};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
//b:用户是选中该条目还是取消该条目
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
checkedItems[i]=b;
}
});
//设置一个确定按钮
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String text="";
for (int j=0;j<items.length;j++){
text +=checkedItems[j]?items[j]+",":"";
}
Toast.makeText(MainActivity.this,text,Toast.LENGTH_SHORT).show();
dialogInterface.dismiss();
}
});
builder.show();
}
分享
Android五大存储之SQLite
Android五大存储之SharedPreference
Android五大存储之外部存储
Android五大存储之内部存储
CSDN:http://write.blog.csdn.net/postlist
github: https://github.com/ITtrap
-
欢迎网友互相交流
QQ: 2632545852
Email: xuchen1009@gmail.com