在APP开发中,我们经常会需要设置点击一个按钮弹出一个弹框,在这个弹框中从事一些选择、筛选的功能,这就是Android中的AlertDialog。在这里,就谈一下如何使用系统自带的AlertDialog类。
首先是最简单的使用,如下:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//创建一个自定义View
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.xxx, null);
builder.setView(view)
//创建一个AlertDialog对象
AlertDialog dialog = builder.create();
dialog.show();
或者使用第二种方式:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.icon);
builder.setTitle("Android Monkey");
builder.setMessage("Edit what you want here");
//创建一个AlertDialog对象
AlertDialog dialog = builder.create();
dialog.show();
很容易看出来,第二种方式是Android使用建造者模式(设计模式的一种)的体现,使我们在不了解细节的情况下使用系统自带的模块,创建一个复杂的对象。
AlertDialog整个弹出框其实包含三个部分,从上到下分别分为区域1、区域2和区域3。区域1是放置title的地方,区域2是最主要的内容部分,在这里我们可以设置一些message信息,或者是定义一组选择框,还可以定义我们自己的布局弹出框。区域3可以使用Button,定义我们的操作按钮。有三种不同的Action Buttons供我们选择。
setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)
//这是一个相当于OK、确定操作的按钮
setNegativeButton (CharSequence text, DialogInterface.OnClickListener listener)
//这是一个相当于取消操作的按钮
setNeutralButton (CharSequence text, DialogInterface.OnClickListener listener)
//这个是相当于一个忽略操作的按钮
AlertBuilder类还带有setItems方法,可以通过调用该方法显示一个列表,供单选或多选。下面是一个示例:
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v)
{
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请选择性别");
final String[] sex = {"男", "女", "未知性别"};
// 设置一个单项选择下拉框
/**
* 第一个参数指定我们要显示的一组下拉单选框的数据集合
* 第二个参数代表索引,指定默认哪一个单选框被勾选上,1表示默认'女' 会被勾选上
* 第三个参数给每一个单选项绑定一个监听器
*/
builder.setSingleChoiceItems(sex, 1, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(MainActivity.this, "性别为:" + sex[which], Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
builder.show();
}
});
在追影APP开发过程中,遇到一个feature是在toolBar上设置一个筛选按钮,弹出的框中有两个列表,每个列表是单选。这里我使用了区域2的自定义布局,设计了第二部分的布局如下:
<!--dialog_selector_layout.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/selectsex"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="性别:"/>
<CheckBox
android:id="@+id/sex_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="全部 "/>
<CheckBox
android:id="@+id/sex_man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="男 "/>
<CheckBox
android:id="@+id/sex_woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="女"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/selectpeople"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="人群:"/>
<CheckBox
android:id="@+id/people_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="全部 "/>
<CheckBox
android:id="@+id/people_photogragher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="摄影师 "/>
<CheckBox
android:id="@+id/people_model"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_state"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/ff_white"
android:text="模特"/>
</LinearLayout>
</LinearLayout>
然后使用如下方式创建Dialog:
AlertDialog builder = new AlertDialog.Builder(MainActivity.this, R.style.AlertDialog).create();
// 通过LayoutInflater来加载一个xml的布局文件作为一个View对象
view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_selector_layout, null);
// 设置我们自己定义的布局文件作为弹出框的Content
builder.setView(view);
关于AlertDialog还遇到另外一种情况就是要根据用户的设置来选择下面的Button是否在点击后要关闭弹出框。如果用户还没有按照提示信息进行相应的操作,在点击确定按钮后弹出框就不会关闭,并给出Toast提示。
我采用了如下实现:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.AlertDialog);
builder.setPositiveButton("确定", null);
// 通过LayoutInflater来加载一个xml的布局文件作为一个View对象
view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_selector_category, null);
// 设置我们自己定义的布局文件作为弹出框的Content
builder.setView(view);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//...省略部分
if ((!checkbox_people_all.isChecked()) && (!checkbox_people_model.isChecked()) &&
(!checkbox_people_photogragher.isChecked())) {
CommonUtils commonUtils = new CommonUtils();
commonUtils.showToast(getContext(), "尚未选择!");
} else if (checkbox_people_all.isChecked()) {
userModel.setUcategory("两者都是");
alertDialog.dismiss();
} else if (checkbox_people_photogragher.isChecked()) {
userModel.setUcategory("摄影师");
alertDialog.dismiss();
} else if (checkbox_people_model.isChecked()) {
userModel.setUcategory("模特");
alertDialog.dismiss();
}
}
//...省略部分
});
Button btnPositive = alertDialog.getButton(android.app.AlertDialog.BUTTON_POSITIVE); btnPositive.setTextColor(getResources().getColor(R.color.ff_white));
btnPositive.setTextSize(15);
以上部分主要代码就是alertdialog.dismiss()
方法,通过使用getButton方法创建的按钮必须要使用dismiss方法来关闭弹框,这样就可以根据逻辑来选择是否关闭弹框了。