- AlertDialog
- ProgressDialog
- DatePickerDialog
- TimePickerDialog
1.AlertDialog
AlertDialog可以在当前的界面弹出一个对话框,这个对话框是置顶于所有界面元素之上的,能够屏蔽掉其他控件的交互能力。一般用于提示一些非常重要的内容或者警告信息。比如为了防止用户误删重要内容,在删除前弹出一个确认对话框。
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setMessage("something important.");
dialog.setCancelable(false);
dialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
break;
default:
break;
}
}
}
2.ProgressDialog
ProgressDialog和AlertDialog类似,都可以在界面上弹出一个对话框,都能够屏蔽掉其他控件的交互能力。不同的是,ProgressDialog会在对话框显示一个进度条。
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
ProgressDialog progressDialog=new ProgressDialog(FirstActivity.this);
progressDialog.setTitle("This is ProgressDialog");
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
break;
default:
break;
}
}
}
3.DatePickerDialog
通过构造方法设置显示样式。可以通过DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)这个构造方法的第二个参数来设置显示样式。
4.TimePickerDialog
对于TimePickerDialog而言,它的样式设置,只有构造函数一种方式,对应的theme参数和DatePickerDialog相同。它内部定义了一个TimePicker,但是没有提供获取的方式。
在构造TimePickerDialog和DatePickerDialog的时候最好使用DialogFragment来进行构造。
本文详细介绍了Android中四种对话框:AlertDialog、ProgressDialog、DatePickerDialog及TimePickerDialog的使用方法与应用场景,包括如何创建对话框及设置样式。
511

被折叠的 条评论
为什么被折叠?



