查找,记忆和使用。
android中的对话框只要是在当前页面弹出一个窗口,用于提示用户重要的信息,并获得焦点;而用户可以与之互动,也可以接
受收用户输入的信息,同时也可以反馈信息给用户。dialog一般是分为图标区,标题区,内容区和按钮区;
创建一个对话框的基本步骤如下;
AlertDialog.Builder buidler = new AlertDialog.Builder(MainActivity.this); //创建一个AlertDialog.Builder的对象
builder.setTitle("标题"); //设置标题一般使用builder的setTitle方法
builder.setIcon("图标"); //设置图标使用builder的setIcon方法
builder.setMessage("内容"); //设置普通的文本对话框的内容;不同的对话框在设置内容的时候会使用builder的不同方法
builder.create().show(); //显示
1.普通的文本对话框:
private void simpleDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("普通对话框");
builder.setMessage("是否确认退出?");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
showToast("您点击了确认按钮");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
showToast("您选择了取消按钮");
}
});
builder.create().show();
}
关于代码中的showToast方法是一个特别封装起来的toast的方法,基本每个人都会的,可以暂时忽略。
2、有一个中立按钮的对话框
private void buttonDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("多按钮对话框");
builder.setMessage("您对***的看法");
builder.setPositiveButton("喜欢", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
showToast("您选择了喜欢");
}
});
builder.setNeutralButton("没感觉", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
showToast("您选择了没感觉");
}
});
builder.setNegativeButton("讨厌", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
showToast("您选择了讨厌");
}
});
builder.create().show();
}
3、列表对话框
private void listDialog() {
final String items[] = {"红","绿","蓝"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("列表对话框");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
showToast("您选择的是:"+items[arg1]);
}
});
builder.create().show();
}
4、单选对话框
private void listDialog() {
final String items[] = {"红","绿","蓝"};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("列表对话框");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
showToast("您选择的是:"+items[arg1]);
}
});
builder.create().show();
}
5、多选对话框
private void multiDialog() {
String items[] = {"红","绿","蓝","白","黑"};
boolean check [] = {false,false,false,false,false};
final List<Integer> mychoose = new ArrayList<Integer>();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("多选对话框");
builder.setMultiChoiceItems(items, check, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
if(arg2)
mychoose.add(arg1);
else
mychoose.remove(arg1);
}
});
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
builder.create().show();
}
6、自定义列表项;因为在布局关联的时候使用的是系统的默认布局,因此得出的结果会和列表对话框类似
private void adapterDialog() {
final String item[] = {"1","2","3"};
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("自定义列表");
builder.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,item),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
showToast("您选择的是:"+item[arg1]);
}
});
builder.create().show();
}
7、自定义布局对话框:要另外写一个布局,将布局相关联
private void viewDialog() {
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.item, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("自定义布局对话框");
builder.setView(layout);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
builder.create().show();
}
8、日期时间对话框:
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.item02_button1:
Calendar c = Calendar.getInstance(); //创建日历对象
new DatePickerDialog(Demo02.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
tv.setText("您选择的是:"+arg1+"年"+arg2+"月"+arg3+"日");
}
}, c.get(Calendar.YEAR), Calendar.MONTH, Calendar.DAY_OF_MONTH).show();
break;
default:
Calendar c2 = Calendar.getInstance();
new TimePickerDialog(Demo02.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
tv.setText("您选择的是:"+arg1+"时"+arg2+"分");
}
},Calendar.HOUR_OF_DAY, Calendar.MINUTE,true).show();
break;
}
}
9、popuoWindow
private void popupWindowDialog() {
View view = getLayoutInflater().inflate(R.layout.item03_01, null);
final PopupWindow win = new PopupWindow(view,111,222);
imb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
win.showAsDropDown(arg0); //设置popupwindow以下拉的方式展现
win.showAtLocation(imb, Gravity.CENTER, 30, 30); //设置对话框的展现位置
}
});
view.findViewById(R.id.item03_btn3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
win.dismiss(); //是popupwindow收回
}
});
}
10、进度对话框,分为环形进度条和线形进度条
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.item04_button1:
ProgressDialog.show(Demo04.this,"环形进度", "您要的消息正在路上,马上就到",false,true);
//false代表不显示进度,true代表触摸对话框之外的位置会停止对话框
break;
default:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setTitle("线型进度条");
dialog.setMessage("稍安勿躁啊,亲");
dialog.setIndeterminate(false);
dialog.setCancelable(true); //触摸对话框外的位置停止对话框
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //设置进度条的类型,水平或垂直等
dialog.show();
break;
}
}
暂且就记录如上对话框。