Base class for Dialogs.
A subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method.
If you want to display a more complex view.
直接创建时,提示:‘AlertDialog(android.content.Context)’ has protected access in ‘android.app.AlertDialog’
使用内部类 AlertDialog.Builder创建
mContext=this;
AlertDialog.Builder dialog=new AlertDialog.Builder(mContext);
dialog.setTitle("");
dialog.setMessage("");
dialog.setPositiveButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
// 重点
dialog.show();
// dialog.setNegativeButton();
// dialog.setNeutralButton();
int checkedItem = 0;
String items[]= new String[]{"a,b,c"};
// params: 选项,默认选中,点击监听
dialog.setSingleChoiceItems(items, checkedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// dialogInterface.dismiss();
}
});
String items2[]= new String[]{"a,b,c"};
boolean[] checkedItem2={false,false,false};
// params: 选项,默认选中,点击监听
dialog.setMultiChoiceItems(items2, checkedItem2, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
// dialogInterface.dismiss();
}
});
复杂布局:
View myView=new TextView(mContext);
FrameLayout fl = (FrameLayout) findViewById(R.id.custom);
fl.addView(myView, new FrameLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
dialog.setView(fl);
ProgressDialog被弃用,被ProgressBar代替。
DatePickerDialog
int year = 0;
// 注意月份是从0开始的
int monthOfYear = 0;
int dayOfMonth = 0;
DatePickerDialog datePickerDialog= new DatePickerDialog(mContext,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
}
},year,monthOfYear,dayOfMonth);
}
// 重点
datePickerDialog.show();
TimePickerDialog
int hourOfDay=0;
int minute=0;
boolean is24HourView=true;
TimePickerDialog timePickerDialog=new TimePickerDialog(mContext, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
}
},hourOfDay,minute, is24HourView);
// 重点
timePickerDialog.show();