那我们先了解什么是AlertDialog?什么是AlertDialog.Builder?且两者有什么区别?
AlertDialog是Dialog的一个直接子类,AlertDialog也是Android系统当中最常用的对话框之一。
一个AlertDialog可以有两个以上的Button,可以对一个AlertDialog设置相应的信息。比如title,massage,setSingleChoiceItems,setPositiveButton,setNegativeButton等等。。。。
但不能直接通过AlertDialog的构造函数来生产一个AlertDialog。研究AlertDialog的源码发现AlertDialog所有的构造方法都是写保护的所以不能通过:AlertDialog alertDialog = new AlertDialog();来得到。AlertDialog构造方法源码如下:
protectedAlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
protectedAlertDialog(Context context,inttheme) {
super(context, theme);
mAlert = newAlertController(context,this, getWindow());
}
protectedAlertDialog(Context context,booleancancelable, OnCancelListener cancelListener) {
super(context, com.android.internal.R.style.Theme_Dialog_Alert);
setCancelable(cancelable);
setOnCancelListener(cancelListener);
mAlert = newAlertController(context,this, getWindow());
}
只能通过:
AlertDialog.Builder alertDialog =newAlertDialog.Builder(this);
来得到。
那就通过一个具体的实例来说说吧(这里用一个最常用的对话框为例):
packagecom.oyah;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.DialogInterface;
importandroid.os.Bundle;
publicclassTestsActivityextendsActivity {
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlertDialog.Builder alertDialog = newAlertDialog.Builder(this);
alertDialog
.setTitle("title")
.setMessage("message")
.setPositiveButton("okBuuon",
newDialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog,
intwhich) {
}
})
.setNegativeButton("exitButton",
newDialogInterface.OnClickListener() {
@Override
publicvoidonClick(DialogInterface dialog,
intwhich) {
}
}).[b]setCancelable(false).[/b]create().show();
}
}
针对AlertDialog中设置了确定和取消按钮,一般来说确定为执行某个动作,取消就是不执行,但是如果用户点击了系统的Back键,此时就会将AlertDialog关闭,而并没有执行预期的取消的操作。
此时需要关注一个方法setCancelable(false) 该方法定义设置该AlertDialog是否可以被Back键取消,如果不设置默认为true
下面是一些扩展:
根据AlertDialog.Builder 创建 相应的 AlertDialog
AlertDialog alertDialogs = alertDialog.create();
用dismiss();方法来清除创建过的AlertDialog
alertDialogs.dismiss();
以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个用于取消AlertDialog 的 Button
定义其布局 main.xml
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
通过LayoutInflater 得到上面 main.xml 布局文件
view =this.getLayoutInflater().inflate(R.layout.main,null);
指定AlertDialog.Builder 所需的布局 并返回目标AlertDialog
alertDialog.setView(view);
alertDialogs= alertDialog.create();
通过 view.findViewById() 得到 目标View 然后设置其内容 如:
TextView title = (TextView) view.findViewById(R.id.title);
title.setTextSize(20);
title.setTextColor(Color.RED);
title.setText("Title");
TextView message = (TextView) view.findViewById(R.id.message);
message.setText("message");
显示对话框 AlertDialog
findViewById(R.id.button).setOnClickListener(newOnClickListener(){
publicvoidonClick(View v) {
// TODO Auto-generated method stub
alertDialogs.show();
}
});
清除对话框 AlertDialog
view.setOnClickListener(newOnClickListener(){
publicvoidonClick(View v) {
// TODO Auto-generated method stub
alertDialogs.dismiss();
}
});