AlertDialog使用步骤:
(1)创建AlertDialog.Builder对象
(2)调用AlertDialog.Builder的setTitle()或setCustomTitle()方法设置标题
(3)调用AlertDialog.Builder的setIcon()方法设置图标
(4)调用AlertDialog.Builder相关方法设置显示内容,
包括:setMessage设置最简单的文本提示信息
setItems 设置内容为简单列表项,调用该方法时需要传入一个数组或者数组资源的资源ID
setSingleChoiceItems 设置内容为单选的列表项,可以传入数组 setMultiChoiceItems 设置内容为多选的列表项
setAdapter 设置内容为自定义列表项
setView 设置内容为任意类型的View,完成一个登录对话框的界面
(5)调用AlertDialog.Builder的setPositiveButton(),setNegativeButton
或setNeutralButton()方法添加多个按钮
(6)调用AlertDialog.Builder的create()方法创建AlterDialog对象
(7)调用AlertDialog的show()方法显示对话框
setCancelable(false):设置是否可以取消对话框,默认为true,点击按钮,回退健或者点击
任何一个地方都会关闭对话框。需要在create之前调用。
AlertDialog.dismiss():取消对话框
AlertDialog.cancel():取消对话框
修改Activity的背景颜色:
getWindow().setBackgroundDrawableResource(int);
修改Activity的背景图片:
getWindow().setBackgroundDrawable(Drawable);
ProgressDialog包含如下的方法:
a)setTitle("提示信息");
b)setMessage(charSequence)设置对话框里显示的消息
c)setMax(int)设置对话框中进度条的最大值
d)setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)设置对话框里进度条的风格
e)setIndeterminate(boolean)设置进度条是否显示不明确值,不明确就是滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人“我正在工作”,但不能提示工作进度到哪个阶段。主要是在进行一些无法确定操作时间的任务时作为提示。而“明确”就是根据你的进度可以设置现在的进度值
f)p.dismiss()关闭对话框
继承关系图:
代码;
privatevoid createCirleProgressAlertDialog() {
ProgressDialogprogressDialog = new ProgressDialog(MainActivity.this);
//progressDialog.setTitle("正在下载");
//progressDialog.setMax(100);
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
}
/**
* 进度条对话框
*/
privatevoid createProgressAlertDialog() {
ProgressDialogprogressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("正在下载");
progressDialog.setMax(100);
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.show();
}
/**
* 时间选择对话框
*/
privatevoid createTimeAlertDialog() {
Calendarc = Calendar.getInstance();
inthours = c.get(Calendar.HOUR_OF_DAY);
intminute = c.get(Calendar.MINUTE);
TimePickerDialogtimePickerDialog = new TimePickerDialog(MainActivity.this,new OnTimeSetListener() {
@Override
publicvoid onTimeSet(TimePicker view, int hourOfDay, int minute){
Stringmin = null;
if(minute < 10) {
min= "0" + minute;
}
Toast.makeText(MainActivity.this,hourOfDay + ":" + min, Toast.LENGTH_SHORT).show();
}
},hours, minute, true);
timePickerDialog.show();
}
/**
* 日期选择对话框
*/
privatevoid createDateAlertDialog() {
Calendarc = Calendar.getInstance();
intyear = c.get(Calendar.YEAR);
intmonth = c.get(Calendar.MONTH);
intday = c.get(Calendar.DAY_OF_MONTH);
//第二个参数是事件监听
DatePickerDialogdatePickerDialog = new DatePickerDialog(MainActivity.this,new OnDateSetListener() {
@Override
publicvoid onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
intmonth = monthOfYear + 1;// 从零开始记月
Toast.makeText(MainActivity.this,year + "年" + month + "月" + dayOfMonth +"日", Toast.LENGTH_SHORT)
.show();
}
},year, month, day);// 默认年月日,后三个参数
datePickerDialog.show();
}
/**
* 自定义对话框
*/
privatevoid createMyAlertDialog() {
AlertDialog.Builderbuilder = new Builder(MainActivity.this);
Viewview = View.inflate(MainActivity.this, R.layout.my_alertdialog_activity,null);
Buttonlogin = (Button) view.findViewById(R.id.my_alertdialog_login_btn);
Buttoncancel = (Button) view.findViewById(R.id.my_alertdialog_cancel_btn);
builder.setView(view);
builder.create().show();
}
/**
* 创建警告对话框
*/
privatevoid createAlertDialog() {
AlertDialog.Builderbuilder = new Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("alertdilog");
builder.setMessage("你确认要删除吗");
//设置左边按钮
builder.setNegativeButton("确认", newOnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"这是确认按钮", Toast.LENGTH_SHORT).show();
MainActivity.this.finish();
}
});
//设置右边按钮
builder.setPositiveButton("取消", newOnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"这是取消按钮", Toast.LENGTH_SHORT).show();
}
});
//返回AlertDialog对象
builder.create();
//展示对话框
builder.show();
}
/**
* 创建单选按钮
*/
privatevoid createOneAlertDialog() {
AlertDialog.Builderbuilder = new Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("单选按钮对话框");
builder.setSingleChoiceItems(newString[] { "男", "女" }, 0, new OnClickListener() {
//第二个参数从0开始,是默认选中
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,which + "", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
//返回AlertDialog对象
builder.create();
//展示对话框
builder.show();
}
其他的方式也可以完成对话框
1)在需要设置成对话框的Activity在AndroidManifest.xml中配置
android:theme="@android:style/Theme.Dialog"
2)PopWindow也可以做类似对话框风格的窗口
值得注意的是:对话框风格的activity和对话框有区别,主要表现在当弹出对话框时父activity
生命周期停留在运行态,而弹出对话框风格的activity时父activity生命周期停留在onPause
Popwindow代码:
public void show(Viewview) {
getPopWindow();
// pop.showAsDropDown(view);
// pop.showAsDropDown(view, 100,100);
pop.showAsDropDown(view, 100, 0,Gravity.CENTER_VERTICAL);
// pop.showAtLocation(view,Gravity.CENTER, 0, 0);
}
private void getPopWindow(){
if (pop!=null) {
pop.dismiss();//消失
return ;
}else {
//初始化一个
initPop();
}
}
private void initPop(){
View view = View.inflate(MainActivity.this,R.layout.pop_contentview, null);
pop = new PopupWindow(view,LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,true);
view.setOnTouchListener(newOnTouchListener() {
@Override
public booleanonTouch(View v, MotionEvent event) {
if(pop!=null&&pop.isShowing()){
pop.dismiss();
pop=null;
}
returnfalse;//事件自己处理
}
});
}