Dialog详解

一、创建的一般步骤:

1)创建构造器

2)给构建器设置属性 如:标题 内容  按钮  设置布局

3)创建dialog

4)显示dialog

代码如下:

AlertDialog.Builder builder = new AlertDialog.Builder(this);//创建构造器

AlertDialog dialog = builder.create();//创建dialog
dialog.show();//显示dialog

二、特性

1)普通对话框:

                builder.setTitle("谁是你的女神???");
		builder.setMessage("章泽天是我的女神。。。。");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setCancelable(false);

2)选择对话框

builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(),
						"你选择的是: " + items[which], 1).show();
			}
		});

3)单选框

builder.setSingleChoiceItems(items, 0,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(getApplicationContext(),
								"你的老婆是: " + items[which], 1).show();
					}
				});

4)多选框

builder.setMultiChoiceItems(items, checkedItems,
				new DialogInterface.OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						Toast.makeText(getApplicationContext(),
								"你选择了 :" + items[which], 1).show();
					}
				});

5)自定义对话框

LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.custom, null);
		builder.setView(view);


三、各种对话框的图示:

1)普通对话框



2)选择对话框

Dialog详解_第1张图片


3)单选框

Dialog详解_第2张图片


4)多选框

Dialog详解_第3张图片


5)自定义对话框

Dialog详解_第4张图片


四、代码实现

1、main.xml

<?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="vertical" >
   
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通对话框"
        android:onClick="comment"
        />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择对话框"
        android:onClick="select"
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选框"
        android:onClick="selectSingle"
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选框"
        android:onClick="selectMultiple"
        />
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义对话框"
        android:onClick="selectSelf"
        />
    
    
</LinearLayout>


2、custom.xml

这个布局文件在自定义对话框时使用

<?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="vertical" >
    
    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        />

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请再次输入密码"
        />
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <Button 
            android:id="@+id/bt_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="确定"
            />
        
         <Button 
            android:id="@+id/bt_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            />
    </LinearLayout>
</LinearLayout>



3、MainActivity

package com.njupt.dialog1;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public void comment(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("谁知你的女神???");
		builder.setMessage("章泽天是我的女神。。。。");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});

		builder.setCancelable(false);

		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void select(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		final String[] items = new String[] { "章泽天", "刘诗诗", "康逸琨" };
		builder.setTitle("请选择你的女神...");
		builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(),
						"你选择的是: " + items[which], 1).show();
			}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void selectSingle(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		final String[] items = new String[] { "章泽天", "刘诗诗", "康逸琨" };
		builder.setTitle("请选择你的老婆是: ");
		builder.setSingleChoiceItems(items, 0,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(getApplicationContext(),
								"你的老婆是: " + items[which], 1).show();
					}
				});

		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void selectMultiple(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		final String[] items = new String[] { "黄俊东", "黄东东", "allen" };
		final boolean[] checkedItems = new boolean[] { true, true, false };

		builder.setTitle("你认为以下谁是帅哥");
		builder.setMultiChoiceItems(items, checkedItems,
				new DialogInterface.OnMultiChoiceClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						Toast.makeText(getApplicationContext(),
								"你选择了 :" + items[which], 1).show();
					}
				});

		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				int i;
				StringBuilder sb = new StringBuilder("");
				for (i = 0; i < items.length; ++i) {
					sb.append(items[i]);
				}

				Toast.makeText(getApplicationContext(), sb.toString(), 1)
						.show();

			}
		});

		AlertDialog dialog = builder.create();
		dialog.show();
	}

	public void selectSelf(View v) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.custom, null);
		builder.setView(view);

		builder.setTitle("自定义框");
		final AlertDialog dialog = builder.create();
		dialog.show();

		Button bt_ok = (Button) view.findViewById(R.id.bt_ok);
		Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);

		bt_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		bt_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



你可能感兴趣的:(Dialog详解)