Android中 AlertDialog 的使用
Android帮助文档中,我们看到关于AlertDialog类的第一段描述如下:
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, look up the FrameLayout called "custom" and add your view to it:
FrameLayout fl =(FrameLayout) findViewById(android.R.id.custom);
fl.addView(myView,newLayoutParams(MATCH_PARENT, WRAP_CONTENT));
简单的将,AlertDialog是类Dialog的子类,它能够呈现给用户多个按钮,显然,它的使用场合会很多。比如我们有时需要实现输入密码对话框,实现查询对话框,实现各种各样的确认|取消对话框等等,这时候我们都可以通过创建自己的AlertDialog对象,实现这些功能。
下面通过实例代码说明AlertDialog的使用。
1 /**
2 * 得到自定义AlertDialog对象
3 *
4 * @param localView
5 * 布局对象
6 * @param title
7 * 对话框标题
8 * @param posivtiveListener
9 * 确定按钮监听器对象
10 * @param negativeListener
11 * 取消按钮监听器对象
12 * @param cancelListener
13 * 取消操作按钮监听器对象
14 * @return AlertDialog对象
15 */
16 public AlertDialog creatDialog(View localView, int dialogTitle,
17 DialogInterface.OnClickListener posivtiveListener,
18 DialogInterface.OnClickListener negativeListener,
19 DialogInterface.OnCancelListener cancelListener) {
20 // 开始设定AlertDialog
21 AlertDialog.Builder builder = new AlertDialog.Builder(this).setView(
22 localView).setTitle(dialogTitle);
23 // 设定PositiveButton按下时的操作
24 builder.setPositiveButton(R.string.ok, posivtiveListener);
25 // 设定NegativeButton按下时的操作
26 builder.setNegativeButton(R.string.cancel, negativeListener);
27 // 设定按下返回时的操作
28 builder.setOnCancelListener(cancelListener);
29 // 创建AlertDialog
30 AlertDialog localAlertDialog = builder.create();
31 // 通过反射修改AlertControler中handler的设置,避免一旦按键,AlertDialog就关闭
32 localAlertDialog = reflectDialog(localAlertDialog);
33 return localAlertDialog;
34 }
上面函数实现了得到自定义AlertDialog对象的功能,输入为布局View对象(可由LayoutInflater从布局文件中得到),对话框标题title以及三个监听器对象。三个监听器对象分别实现了确定、取消、返回时的功能。我们需要根据具体的应用实现DialogInterface.OnCancelListener,将对应的监听器类对象传入函数中。
创建AlertDialog对象我们使用AlertDialog.Bulder内部类,通过调用其中的各种方法,创建功能强大的AlertDialog,帮助文档中有很详细的说明,这里不再赘述。
在上段代码的最后我们看到,将创建的localAlertDialog对象传入reflectDialog()函数中,其目的就是为了更改Android对AlertDialog的默认设置,即一旦用户点击按钮,便在最后调用dialog.dismiss()方法,使对话框消失。我们在很多情况下是不想要对话框消失的,比如密码输入错误需要提示用户重新输入时。下面这段代码是使用Java反射机制,修改AlertDialog内部定义ButtonHandler,取消一旦按钮按下对话框自动关闭的设置。
1 /**
2 * 通过反射修改AlertControler中handler的设置,防止一旦按键,AlertDialog就关闭
3 *
4 * @param alertDialog
5 * @return AlertDialog对象
6 */
7 public AlertDialog reflectDialog(AlertDialog alertDialog) {
8 AlertDialog localAlertDialog = alertDialog;
9 try {
10
11 Field field = localAlertDialog.getClass()
12 .getDeclaredField("mAlert");
13 field.setAccessible(true);
14 // 获得mAlert变量的值
15 Object obj = field.get(localAlertDialog);
16 field = obj.getClass().getDeclaredField("mHandler");
17 field.setAccessible(true);
18 // 修改mHandler变量的值,使用新的ButtonHandler类
19 field.set(obj, new ButtonHandler(localAlertDialog));
20 } catch (Exception e) {
21 e.printStackTrace();
22 }
23 return localAlertDialog;
24 }
关于利用Java反射技术阻止通过按钮关闭Android对话框的内容,在《利用Java反射技术阻止通过按钮关闭Android对话框》中详细说明。
通过creatDialog函数 ,我们得到了我们设定好的AlertDialog对象,再通过调用show()方法就能将对话框显示出来。
在这里还是建议使用Activity中的onCreateDialog()方法,通过自定义的Dialog id创建并显示对应的对话框,尤其在有较多对话框的情况下,这样,代码更加清晰,便于维护和管理。
浙公网安备 33010602011771号