一、对话框的分类android
<1>AlertDialog 警告对话框(提示对话框)app
(1)父类:android.app.Dialogide
(2)建立AlertDialog对话框的步骤布局
a.建立AlertDialog.Builder对象,该对象能建立AlertDialog;ui
AlertDialog alertDialog = null; AlertDialog.Builder builder = new Builder(MainActivity.this);
b.调用Builder对象的方法设置图标、标题、内容、按钮等;this
builder.setTitle("警告对话框")// 设置标题 .setIcon(R.drawable.icon18)// 设置标题图标 .setMessage("肯定要删除吗?")// 设置标题文本内容 .setPositiveButton("肯定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 点击肯定按钮要作的事 Toast.makeText(MainActivity.this, "肯定",Toast.LENGTH_SHORT).show();}) .setNegativeButton("取消", null) .setNeutralButton("其余", null);
c.调用Builder对象的create()方法建立AlertDialog对话框.setCanceledOnTouchOutside(false):点击对话框之外对话框不消失code
// 经过builder对象建立对话框对象 alertDialog = builder.create();
d.调用AlertDialog的show()方法来显示对话框xml
// 显示对话框 alertDialog.show();
<2>ProgressDialog 进度对话框对象
(1)父类:android.app.AlertDialogutf-8
(2)建立ProgressDialog对话框的步骤:
a.实例化ProgressDialog,建立出ProgressDialog对象
b.调用该对象的方法设置图标、标题、内容、按钮等
c.调用 ProgressDialog 对象的show()方法显示出 ProgressDialog 对话框
<3>DatePickerDialog 日期选择对话框
(1)父类:android.app.AlertDialog
(2)建立DatePickerDialog对话框的步骤:
a.实例化DatePickerDialog,建立出 DatePickerDialog对象
DatePickerDialog dialog = new DatePickerDialog(this,new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //选择日期以后调用的方法 注意:参数monthOfYear是0~11 Toast.makeText(MainActivity.this, year+":"+(monthOfYear+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show(); } }, year, month, day);
b.调用DatePickerDialog对象的show()方法显示出DatePickerDialog对话框
dialog.show();
c.绑定监听器:OnDateSetListener()
Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { //选择日期以后调用的方法 注意:参数monthOfYear是0~11 Toast.makeText(MainActivity.this, year+":"+(monthOfYear+1)+":"+dayOfMonth, Toast.LENGTH_SHORT).show(); }
<4>TimerPickerDialog 时间选择对话框
<5>自定义对话框(登陆对话框、关于对话框)
(1)AlertDialog——自定义对话框的建立步骤:
a.建立AlertDialog.Builder对象
AlertDialog.Builder builder = new Builder(this);
b.设置对话框的标题、按钮等(既能够使用系统自带的,也能够自定义)
c.自定义布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" android:textSize="30sp" android:gravity="center" android:padding="10dp"/> <TextureView android:layout_width="match_parent" android:layout_height="1dp" android:background="#ccc"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="肯定要删除吗?" android:gravity="center" android:padding="15dp" android:textSize="20sp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/btnSure" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="肯定" /> <Button android:id="@+id/btnClean" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout>
d.使用LayoutInflater 的 inflater()方法填充自定义的布局文件,返回view对象。用该对象的findViewById()方法加载自定义布局上全部控件;
// 获取布局对象的两种方法 View view2 = LayoutInflater.from(this).inflate(R.layout.dialog_layout,null); // view2=getLayoutInflater().inflate(R.layout.dialog_layout, null);
e.调用Builder对象的setView()方法加载view对象;
builder.setView(view2);// 设置对话框要显示的布局 Button btnSure = (Button) view2.findViewById(R.id.btnSure);
f.调用Builder对象的create()方法建立AlertDialog对话框;
customDialog = builder.create();
g.调用AlertDialog的show()方法来显示对话框
customDialog.show();
<6>列表对话框
(1)普通列表对话框
(2)单选列表对话框
(3)多选列表对话框
(4)带图标的列表对话框