每一个应用都离不开对话框的使用,对话框的形式多种多样,可以使用简单的系统对话框,也可以自定义布局文件来设置对话框,自定义布局文件可以在对话框中加入各种控件,比较常见的如:性别、爱好、地区等的选择。今天我介绍三种种对话框,第一种是使用系统的简单对话框,第二种是使用布局文件的对话框,第三种是在布局文件中使用
RadioButton控件的性别选择对话框,由于没有图片等资源,制作比较粗糙,主要是让大家看到效果。
首先是使用系统的对话框:
public void showDialog(){ AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("提示"); dialog.setIcon(R.drawable.ic_launcher); dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { System.exit(0); } }); dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.setMessage("您确定要退出应用?"); dialog.show(); }
效果如下:
然后介绍使用布局文件的对话框:
public void showDialogTwo(){ Builder builder = new Builder(this); final AlertDialog dialog = builder.create(); dialog.show(); dialog.getWindow().setContentView(R.layout.dialog); TextView mSure = (TextView) dialog.findViewById(R.id.sure); TextView mCancel = (TextView) dialog.findViewById(R.id.cancel); mSure.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { System.exit(0); } }); mCancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); }
布局文件为:
<?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:background="#f2f2f2" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:gravity="center_vertical" android:text="提示" android:textSize="18sp" android:textColor="#000000" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginTop="5dp" android:background="#d7d7d7" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:text="您确定要对出程序?" android:textColor="#000000" android:layout_gravity="center_horizontal" android:textSize="18sp" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginTop="5dp" android:background="#d7d7d7" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/sure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:gravity="center" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:text="确定" android:textColor="#000000" android:textSize="18sp" /> <View android:layout_width="2dp" android:layout_height="match_parent" android:background="#d7d7d7" /> <TextView android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.5" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:gravity="center" android:text="取消" android:textColor="#000000" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
效果如下:
最后界面布局文件中使用RadioButton的对话框:
public void showSexChange(){ Builder builder = new Builder(this); final AlertDialog dialog = builder.create(); dialog.show(); dialog.getWindow().setContentView(R.layout.sex_change); RadioButton mButton = (RadioButton) dialog.findViewById(R.id.radioButton1); RadioButton mButton2 = (RadioButton) dialog.findViewById(R.id.radioButton2); mButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(getBaseContext(), "您选择了:男", 1).show(); dialog.dismiss(); } }); mButton2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Toast.makeText(getBaseContext(), "您选择了:女", 1).show(); dialog.dismiss(); } }); }
布局文件:
<?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:background="#f2f2f2" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical" android:layout_marginLeft="20dp" android:layout_marginRight="50dp" android:layout_marginTop="5dp" android:text="性别选择" android:textSize="18sp" android:textColor="#000000" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:layout_marginTop="5dp" android:background="#d7d7d7" /> <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" android:layout_marginLeft="15dp" android:textColor="#000000" android:textSize="18sp"/> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:text="女" android:textColor="#000000" android:textSize="18sp"/> </LinearLayout>
效果如下:
效果已经出来,同时有的应用可能需要弹出对话框后用户点击空白区域不让对话框消失的要求,这个也很简单,只需在代码中加入:
dialog.setCancelable(false);