AlertDialog是用来和用户交流互动的很好的工具,善用之可以为应用程序增色。有人认为它简单”不就一个对话框么“,我觉得技术是需要严谨甚至谦卑。手机屏幕是个寸土必争之地,那么既然点进来看此文了,说明还是对AlertDialog想了解更多的好学人士。此文的目标:不再搜索”Android AlertDialog“!
先来看一个最简单的AlertDialog:
其实,我觉得这个最基本的AlertDialog已经足够好看的了。下面是实现代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/** AlertDialog.Builder 是用来创建AlertDialog的 */
AlertDialog.Builder builder =
new
AlertDialog.Builder(MainActivity.
this
);
builder
//给builder set各种属性值
.setIcon(R.drawable.blink)
//继续set
.setMessage(getString(R.string.alert_dialog_message))
.setPositiveButton(
"确定退出"
,
new
OnClickListener() {
//确定按钮
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
MainActivity.
this
.finish();
System.exit(
0
);
}
})
.setNegativeButton(
"我按错了"
,
new
OnClickListener() {
//取消按钮
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
dialog.dismiss();
}
})
.show();
//显示AlertDialog
|
这里,也许会奇怪,为什么没有直接见到AlertDialog呢?而是用了一个Builder,set了一些值之后直接.show()就出来了?
如果有这么浓厚的好奇心,还是要看一下AlertDialog的源码:
1
|
public
class
AlertDialog
extends
Dialog
implements
DialogInterface
|
首先AlertDialog继承自Dialog实现了DialogInterface接口,那么使用的时候也可以考虑用一下Dialog的函数。
1
|
protected
AlertDialog(Context context)
|
构造方法都是用protected来修饰的,说明我们没有办法直接new AlertDialog(),Google给我们提供了一个AlertDialog的内部类AlertDialog.Builder来实现:
1
|
public
static
class
Builder
|
很欣喜的看到public修饰符了,这也就是上文使用AlertDialog.Builder的原因。
对于AlertDialog.Builder的理解,从字面上看出,它是用来构建AlertDialog的,可以概括一下,它是为AlertDialog做一些准备工作的。下面我们来看看AlertDialog对象的使用,还是先看效果,这才有兴趣往下看呐:
代码只是略加改动,体现了AlertDialog对象的作用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/** AlertDialog.Builder 是用来创建AlertDialog的 */
AlertDialog.Builder builder =
new
AlertDialog.Builder(MainActivity.
this
);
AlertDialog alertDialog =
builder
//给builder set各种属性值
.setIcon(R.drawable.blink)
//继续set
.setTitle(getString(R.string.alert_dialog_message))
.setMessage(getString(R.string.alert_dialog_message))
.setPositiveButton(
"确定退出"
,
new
OnClickListener() {
//确定按钮
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
MainActivity.
this
.finish();
System.exit(
0
);
}
})
.setNegativeButton(
"我按错了"
,
new
OnClickListener() {
//取消按钮
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
dialog.dismiss();
}
})
.create();
//创建AlertDialog对象
alertDialog.setMessage(
"AlertDialog对象:\n\t\t"
+ getString(R.string.alert_dialog_message));
alertDialog.show();
|
自定义AlertDialog,我觉得效果还不如默认的好:
布局文件alert_dialog_custom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"horizontal"
>
<
TextView
android:id
=
"@+id/alert_dialog_message"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:text
=
"@string/alert_dialog_message"
android:drawableLeft
=
"@drawable/blink"
android:gravity
=
"center_vertical"
/>
<
Button
android:id
=
"@+id/alert_dialog_btn"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/more"
android:text
=
"@string/alert_dialog_btn"
/>
</
LinearLayout
>
|
使用自定义布局很简单:
1
|
builder.setView(LayoutInflater.from(MainActivity.
this
).inflate(R.layout.alert_dialog_custom,
null
));
|
AlertDialog.Builder提供了setView的方法来使用自定义布局。
AlertDialog.Builder的setView方法是在AlertDialog的Message下面提供了一个自定义布局的空间,并不能改变整个AlertDialog的风格。下面请看改变整体风格的AlertDialog:
布局文件可以自己任意发挥,主要还是看:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
AlertDialog alertDialog =
new
AlertDialog.Builder(MainActivity.
this
).create();
//Builder直接create成AlertDialog
alertDialog.show();
//AlertDialog先得show出来,才能得到其Window
Window window = alertDialog.getWindow();
//得到AlertDialog的Window
window.setContentView(R.layout.alert_dialog_custom);
//给Window设置自定义布局
View layout = LayoutInflater.from(MainActivity.
this
).inflate(R.layout.alert_dialog_custom,
null
);
//从xml中inflate过来
TextView dialogMsg = (TextView) window.findViewById(R.id.alert_dialog_message);
//从Window中findView
dialogMsg.setOnClickListener(
new
View.OnClickListener() {
//设置监听
@Override
public
void
onClick(View v) {
MainActivity.
this
.finish();
System.exit(
0
);
}
});
|
关于AlertDialog,我想到的就这么多了,抛砖引玉。
本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/1219291,如需转载请自行联系原作者