Android对话框_详解


android中对话框是很是经常使用的控件之一, google也提供了各类自定义对话框. 我以为好多人都太模糊. 因此我全面的总结下.android

对话框的几种实现方式:编程

  1. Activity
  2. Popwindow 这个我以前详细讲过能够去看. popwindow详解
  3. Dialog
  4. Fragment

Dialog这个类的使用方式是由Activity来管理和建立. 可是其使用方法已经被废弃. 因此如今通常直接使用其子类. (这一段我也不肯定, 网上对于Dialog的讲解不多). 因此Dialog我不讲了.数组

关键类缓存

  • AlertDialog
  • ProgressDialog
  • DialogFragment
  • Activity

[TOC]ide

示例

这里有个关键类: AlertDialog.Builder. 属于构造器模式用法.布局

这里演示最简单的对话框post

代码动画

 
     
1
2
3
4
 
     
AlertDialog.Builder builder = new AlertDialog.Builder( this);
builder.setTitle( "标题");
builder.setMessage( "信息");
builder.show();

方法介绍

上面介绍了最基本的AlertDialog的使用. 可是在开发中须要更复杂的功能实现. 因此我会介绍下全部的方法ui

Builder是属于AlertDialog的内部类. 负责建立AlertDiglog的构造器. 因此属于链式编程.this

正由于是构造器模式, AlertDialog的全部方法你均可以直接忽略, Builder已经实现了全部的功能. 而且AlertDialog是Protected权限没法直接建立对象的.

肯定和取消/中立按钮

虽然叫作肯定和取消按钮, 不过你设置成别的名称或者功能均可以.

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
 
     
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) {
}
});

左边按钮(取消)

 
     
1
2
3
4
5
 
     
AlertDialog. Builder setPositiveButton (int textId, // 字符串数组资源id
DialogInterface.OnClickListener listener)
AlertDialog.Builder setPositiveButton (CharSequence text, // 字符串
DialogInterface.OnClickListener listener)

右边按钮(肯定)

 
     
1
2
3
4
5
 
     
AlertDialog. Builder setNegativeButton (CharSequence text,
DialogInterface.OnClickListener listener)
AlertDialog.Builder setNegativeButton (int textId,
DialogInterface.OnClickListener listener)

中立按钮(随便你写啥). 这个按钮位于取消和肯定中间的一个.

 
     
1
2
3
4
5
 
     
AlertDialog. Builder setNeutralButton (CharSequence text,
DialogInterface.OnClickListener listener)
AlertDialog.Builder setNeutralButton (int textId,
DialogInterface.OnClickListener listener)

传统选择对话框

注意全部的条目选择方法都不能和setMessage同时使用. 不然无效. 而且选中任何条目都将关闭对话框.

 
     
1
2
3
4
5
 
     
AlertDialog. Builder setItems (int itemsId,
DialogInterface.OnClickListener listener)
AlertDialog.Builder setItems (CharSequence[] items,
DialogInterface.OnClickListener listener)

MaterialDesign单选对话框

该对话框和传统的单选对话框的区别是选中条目后能够不自动关闭对话框. 而且能够设置默认选中的条目.

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
     
AlertDialog. Builder setSingleChoiceItems (int itemsId, // 资源数组id
int checkedItem, // 选中状态的条目索引. 若是默认不选中任何, 写 -1
DialogInterface.OnClickListener listener)
AlertDialog.Builder setSingleChoiceItems (ListAdapter adapter, // 前面提过的适配器
int checkedItem,
DialogInterface.OnClickListener listener)
AlertDialog.Builder setSingleChoiceItems (Cursor cursor, // 前面提过的游标
int checkedItem,
String labelColumn,
DialogInterface.OnClickListener listener)
AlertDialog.Builder setSingleChoiceItems (CharSequence[] items, // 字符串数组
int checkedItem,
DialogInterface.OnClickListener listener)

多选对话框

 
     
1
2
3
4
5
6
7
8
9
10
11
12
 
     
AlertDialog. Builder setMultiChoiceItems (CharSequence[] items, // 条目数组
boolean[] checkedItems, // 选择状态数组 ,若是不想默认选中任何, 可写 null
DialogInterface.OnMultiChoiceClickListener listener)
AlertDialog.Builder setMultiChoiceItems (int itemsId,
boolean[] checkedItems,
DialogInterface.OnMultiChoiceClickListener listener)
AlertDialog.Builder setMultiChoiceItems (Cursor cursor, // 采用游标的方式
String isCheckedColumn,
String labelColumn,
DialogInterface.OnMultiChoiceClickListener listener)

参数介绍:

  • items : 没什么好讲的. 条目的内容
  • checkedItems 这是一个布尔类型数组. 对应items的选择状态
  • listener 回调监听器

示例

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
     
final String[] itemTitles = { "象拔蚌", "鲍鱼", "寿桃包"};
builder.setMultiChoiceItems(itemTitles, null, new DialogInterface.OnMultiChoiceClickListener() {
/**
* 在选中对话框条目的时候回调
* @param dialog 对话框, 可用于在该回调方法内部调用dismiss关闭对话框
* @param which 当前被选中的条目索引
* @param isChecked 被选择状态
*/
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// 吐司
Toast.makeText(MainActivity. this, itemTitles[which], Toast.LENGTH_SHORT).show();
dialog.dismiss(); // 关闭对话框
}
});

适配器建立对话框

至关于使用ListView的适配器ListAdapter来设置选择条目的显示内容. 能够显示更加丰富的内容.

 
     
1
2
 
     
AlertDialog. Builder setAdapter (ListAdapter adapter,
DialogInterface.OnClickListener listener)

游标建立对话框

 
     
1
2
3
 
     
AlertDialog. Builder setCursor (Cursor cursor,
DialogInterface.OnClickListener listener,
String labelColumn)

建立和显示

 
     
1
2
3
 
     
AlertDialog create () // 建立只是返回一个AlertDialog对象. 并不会显示该对话框
AlertDialog show () // 直接显示对话框

获得上下文

获得建立AlertDialog时所传入的上下文对象.

 
     
1
 
     
Context getContext ()

是否可取消

默认为true, 即点击对话框外部会关闭对话框. 若是false则不会关闭

 
     
1
 
     
AlertDialog. Builder setCancelable (boolean cancelable)

标题

 
     
1
2
3
 
     
AlertDialog. Builder setTitle (CharSequence title)
AlertDialog.Builder setTitle (int titleId)

上面的方法标题只是文字的形式而已. 而下面的方法能够在标题的位置自定义任何view对象来显示.

 
     
1
 
     
AlertDialog. Builder setCustomTitle (View customTitleView)

标题图标

即在标题的左边加上一个图片做为图标显示

 
     
1
2
3
 
     
AlertDialog. Builder setIcon (Drawable icon)
AlertDialog.Builder setIcon (int iconId)

还有一个经过主题的属性来设置对话框图标. 我不懂

 
     
1
 
     
AlertDialog. Builder setIconAttribute (int attrId)

自定义对话框显示

能够自定义对话框显示任何内容. 注意即便你自定义了对话框. 你使用设置肯定和取消按钮

 
     
1
2
3
 
     
AlertDialog. Builder setView (int layoutResId) // 布局文件便可
AlertDialog.Builder setView (View view) // View对象

监听器

取消监听器

取消监听器是点击对话框外部的方式关闭了对话框. 调用dismiss方法关闭的时候不会回调

 
     
1
 
     
AlertDialog. Builder setOnCancelListener (DialogInterface.OnCancelListener onCancelListener)

关闭监听器

该监听器是在AlertDialog调用了dismiss()方法或者点击了对话框外部都会回调的监听器.

 
     
1
 
     
AlertDialog. Builder setOnDismissListener (DialogInterface.OnDismissListener onDismissListener)

ListAdapter条目监听器

该方法是你将ListAdapter设为对话框条目显示的时候使用的方法

 
     
1
 
     
AlertDialog.Builder setOnItemSelectedListener (AdapterView.OnItemSelectedListener listener)

按键监听器

能够接受按键的事件. 可用于在弹出对话框后屏蔽按键.

 
     
1
 
     
AlertDialog. Builder setOnKeyListener (DialogInterface.OnKeyListener onKeyListener)

进度对话框

除了以上的警示对话框的使用外. 还提供一种单独的对话框样式. 进度条对话框. 进度条是计算机应用历史上一个伟大的发明.

 
     
1
2
3
4
 
     
ProgressDialog progressDialog = new ProgressDialog( this); // 建立进度对话框对象
progressDialog.setTitle( "标题"); // 设置标题
progressDialog.setMessage( "加载中..."); // 设置消息
progressDialog.show(); // 显示进度条

除了上面这种默认进度条样式, Google还提供设置样式水平进度条

 
     
1
2
 
     
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//ProgressDialog.STYLE_SPINNER 旋转进度条, 默认就是这种样式

方法介绍

ProgressDialog是AlertDialog的子类. 继承了其全部方法. 因此这里我只讲新增的方法.

进度设置

既然是进度对话框固然提供设置进度的方法, 默认的旋转进度条样式是没法设置进度的. 该方法必须在show()后面执行才生效.

 
     
1
2
3
 
     
int getProgress ()
void setProgress (int value)

最大进度

 
     
1
2
3
 
     
void setMax (int max)
int getMax ()

显示

能够用父类的show()方法. 也能够用ProgressDialog新增的静态方法直接一行代码显示.相似于Toast的用法.

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
     
ProgressDialog show (Context context, // 上下文
CharSequence title, // 标题
CharSequence message) // 消息内容
ProgressDialog show (Context context,
CharSequence title,
CharSequence message,
boolean indeterminate)
ProgressDialog show (Context context,
CharSequence title,
CharSequence message,
boolean indeterminate,
boolean cancelable)
ProgressDialog show (Context context,
CharSequence title,
CharSequence message,
boolean indeterminate,
boolean cancelable,
DialogInterface.OnCancelListener cancelListener)

次要进度

相似于你看视频的时候的缓存进度条. 比主进度条的颜色浅一些.

 
     
1
2
3
 
     
void setSecondaryProgress (int secondaryProgress)
int getSecondaryProgress ()

增加进度

和setProgress的区别是该修改是在原有的进度基础上增长或者减小. 而且不须要在意在show()方法以前仍是以后.

 
     
1
2
3
 
     
void incrementProgressBy (int diff)
void incrementSecondaryProgressBy (int diff)

不肯定状态进度条

该效果只针对水平进度条. 进度条的状态显示在不断地变化.

 
     
1
2
3
4
5
 
     
void setIndeterminate (boolean indeterminate) // 默认为false. true开启效果
boolean isIndeterminate () // 是否处于不肯定状态
void setIndeterminateDrawable (Drawable d) // 设置一个图片做为不肯定进度的显示

进度样式

 
     
1
 
     
void setProgressStyle (int style)

包括两个参数:

  1. STYLE_HORIZONTAL 水平进度条
  2. STYLE_SPINNER 环形进度条

设置进度图片

该方法只有在ProgressDialog为水平样式时才有效.

 
     
1
 
     
void setProgressDrawable (Drawable d)

格式化

 
     
1
2
3
 
     
void setProgressNumberFormat (String format)
void setProgressPercentFormat (NumberFormat format)

ProgressBar

ProgressBar是做为控件使用的进度条. 且只能写在布局文件中使用. ProgressDialog的实现原理就是建立一个包含了ProgressDialog的View显示.

进度条对话框还能够在布局文件中直接写入做为控件, 同时该类只能够在布局中使用才会显示. 系统提供的水平进度样式. StyleProgressBar.Horizontal

 
     
1
2
3
4
5
6
7
8
 
     
<ProgressBar
style= "@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:progress= "50"
android:max= "100"
android:layout_gravity= "center"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"/
/>

顺带一提, 进度条不能在子线程开启(show)可是能够在子线程关闭(dismiss).

DialogFragment

该类属于Fragment的子类, 也是官方推荐的对话框.

因为Dialog对象没有生命周期方法, 因此在横竖屏切换等会致使重建生命周期的状况出现时会丢失对话框.

若是没有以上要求建议仍是Dialog对话框. 由于更加方便简单.

和通常的Fragment同样用法.

 
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
     
public class CustomDialogFragment extends DialogFragment {
/**
* 和通常Fragment的onCreate方法同样
*
* @param inflater
* @param container
* @param savedInstanceState
* @return 返回View对象
*/
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
/**
* 用于在Fragment里面嵌套Dialog, 和上面的方法两选一实现便可.
* 若是两个都写了只有这个方法生效
*
* @param savedInstanceState
* @return 返回Dialog对象
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
}

在Activity里面显示

 
     
1
 
     
new Dialog().show(getSupportFragmentManager(), "bag");

声明周期方法

这里介绍的都是用于重写的周期回调方法. 注意DialogFragment是Fragment的子类. 包括Fragment的全部生命周期

关闭

当dismiss方法关闭对话框时回调该方法

 
     
1
 
     
void onDismiss (DialogInterface dialog)

取消

点击DialogFragment外部取消对话框回调的方法

 
     
1
 
     
void onCancel (DialogInterface dialog)

调用方法

用于调用的方法

显示对话框

  • 通常的用法显示DialogFragment, 会做为通常的Fragment嵌套在Activity中
  • 使用如下方法将把DialogFragment做为对话框弹窗显示
 
     
1
2
3
4
5
 
     
void show (FragmentManager manager,
String tag) // 标签. 这些参数其实只要会Fragment都能看懂.
int show (FragmentTransaction transaction,
String tag)

关闭对话框

 
     
1
 
     
void dismiss ()

是否可取消

即点击对话框外部是否能够关闭对话框

 
     
1
2
 
     
void setCancelable (boolean cancelable)
boolean isCancelable ()

获得对话框对象

 
     
1
 
     
Dialog getDialog () // 获得onCreateDialog方法返回的Dialog对象

是否做为对话框显示

若是设为false则将和Fragment同样建立布局而不是对话框. 这个方法须要在onCreateDialog方法以前执行才有效. 推荐onCreate方法里面执行 . 不过若是你设为false, DialogFragment的show方法将无效.

该方法没啥卵用.

 
     
1
2
3
 
     
boolean getShowsDialog ()
void setShowsDialog (boolean showsDialog) // 默认为true

主题样式

必须在onCreateDialog方法执行前调用. 即onCreate方法里面调用.

 
     
1
2
 
     
void setStyle (int style, // 样式
int theme) // 主题, 若是0则使用默认主题

style支持四种参数值:

  1. STYLE_NORMAL 普通
  2. STYLE_NO_FRAME 无边框
  3. STYLE_NO_INPUT 没法获得焦点. 将没法输入或者点击
  4. STYLE_NO_TITLE 无标题

获得DialogFragment的主题

对应获得setStyle()方法设置的主题.

 
     
1
 
     
int getTheme ()

取消状态丢失

详情看Fragment的commitAllowingStateLoss方法

 
     
1
 
     
void dismissAllowingStateLoss ()

Activity

Activity做为对话框. 只须要在主题中继承dialog的主题便可. 而后正常打开该activity便可.

示例

 
     
1
2
3
4
 
     
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent </item>
<item name="android:windowNoTitle">true </item>
</style>

这里我取消了标题栏和背景透明.

 注意activity的根布局千万不要设置为match_parent全屏显示.

总结

特色介绍:

  • Dialog 这类的实现google官方不推荐由于没有生命周期. 若是意外销毁会致使数据的丢失. 不过使用简单. 若是没什么特别的要求就用这个吧
  • DialogFragment 能够算的上是最佳实现. 若是对话框界面相对复杂的话能够用这个.
  • Activity 自带渐变的过渡动画, 懒得写过渡动画就用这个吧.
  • popwindow 能够附着到其余控件周围.