在android api中有很多控件都继承自view的,但是dialog并不是继承自view的,还有anctivity,这里简单的只讲下dialog
dialog直接继承自Object类。
它有如下构造函数:
Dialog(Context context, int theme)
Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)
一般用的最多的就是第一个了,还有它的show()函数 cancel() dismiss()
使用cancel dismiss 会关闭dialog,但是不会清理资源,还在堆里,必须清理,否则会出现问题的
它的子类有AlertDialog,CharacterPickerDialog,这个CharacterPickerDialog我没有用过
一般我使用AlertDialog,而且不是直接创建对象,使用它的一个内部类来返回实例
onCreateDialog()函数:
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1: {
return new AlertDialog.Builder(this).setIcon(R.drawable.icon)
.setTitle(R.string.menu_group_longclick).setItems(
R.array.menu_group_longclick_array,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
onMenuItemClick(
Globals.DIALOG_GROUPS_LONGCLICK,
which);
// removeDialog(1);
//建议使用,清楚缓存
}
}).create();
}
case 2:{
return new AlertDialog.Builder(this).setIcon(R.drawable.icon)
.setTitle(R.string.menu_group_longclick).setItems(
R.array.menu_group_longclick_array,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
onMenuItemClick(
Globals.DIALOG_GROUPS_LONGCLICK,
which);
//建议使用,清楚缓存
// removeDialog(2);
}
}).create();
}
}
}
然后使用showDialog(1 or 2)弹出Dialog,它有两个按钮,是源代码里写好的,风格也可以自定义,也可以将他们设置为false:
dialog.getButton(AlertDialog.Button_POSITIVE).setEnable(false);
AlertDialog有三个子类:DatePickerDialog TimePickerDialog ProgressDialog ,同样也可在oncreateDialog(int id)里面返回实例
case Globals.DIALOG_MYINFO_PROGRESS :{
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.msg_save_progress));
return mProgressDialog;
}