Android AlertDialog的一切

AlertDialog是用来和用户交流互动的很好的工具,善用之能够为应用程序增色。有人认为它简单”不就一个对话框么“,我以为技术是须要严谨甚至谦卑。手机屏幕是个寸土必争之地,那么既然点进来看此文了,说明仍是对AlertDialog想了解更多的好学人士。文本的目标:不想搜索”Android AlertDialog“! java

先来看一个最简单的AlertDialog: android

其实,我以为这个最基本的AlertDialog已经足够好看的了。下面是实现代码: ide

/** 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来实现: ui

1
public static class Builder

很欣喜的看到public修饰符了,这也就是上文使用AlertDialog.Builder的缘由。 this

对于AlertDialog.Builder的理解,从字面上看出,它是用来构建AlertDialog的,能够归纳一下,它是为AlertDialog作一些准备工做的。下面咱们来看看AlertDialog对象的使用,仍是先看效果,这才有兴趣往下看呐: spa


代码只是略加改动,体现了AlertDialog对象的做用:

/** 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 code

<?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>
使用自定义布局很简单:
builder.setView(LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_dialog_custom, null));

AlertDialog.Builder提供了setView的方法来使用自定义布局。

AlertDialog.Builder的setView方法是在AlertDialog的Message下面提供了一个自定义布局的空间,并不能改变整个AlertDialog的风格。下面请看改变总体风格的AlertDialog:

布局文件能够本身任意发挥,主要仍是看:

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);
			}
		});
相关文章
相关标签/搜索