【Android学习笔记】自定义AlertDialog对话框


(1)布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="70dp"
        android:layout_marginTop="62dp"
        android:text="警告对话框" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="64dp"
        android:text="多选对话框" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="60dp"
        android:text="单选对话框" />

</RelativeLayout>

(2)类的文件:

package com.example.alertdialog;

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

/**
 * 警告对话框的使用
 * 
 * @author piaodangdehun
 * 
 */
public class MainActivity extends Activity {

	private Button button1;// 警告对话框
	private Button button2;// 多选对话框
	private Button button3;// 单选对话框
	private AlertDialog.Builder builder; // 为button1设定的对话框,当有一个对话框的时候可以使用全局的定义

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		button3 = (Button) this.findViewById(R.id.button3);

		
		/*
		 * 1.警告对话框的设定
		 */
		builder = new AlertDialog.Builder(this);
		builder.setTitle("提示框");
		builder.setMessage("你确定要删除吗?");
		builder.setIcon(R.drawable.ic_launcher); // 设置图片

		/*
		 * 确定对话框
		 */
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "确认删除", 1).show();
			}
		});
		/*
		 * 取消对话框
		 */
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "取消删除", 1).show();
			}
		});
		/*
		 * 忽略对话框
		 */
		builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(MainActivity.this, "忽略删除", 1).show();
			}
		});
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				builder.show();
			}
		});

		/*
		 * 2.多选对话框
		 * 当有多个对话框的时候在具体的时间内不进行声明定义
		 */
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/*
				 * 对话框的创建
				 */
				AlertDialog.Builder builder = new AlertDialog.Builder(
						MainActivity.this);
				builder.setTitle("请选择爱好");
				final String[] hobby = { "上网", "打游戏", "聊天" };

				/*
				 * 选择的列表
				 */
				builder.setMultiChoiceItems(hobby, new boolean[] { true, false,
						false },
						new DialogInterface.OnMultiChoiceClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which, boolean isChecked) {
								if (isChecked) {
									Toast.makeText(MainActivity.this,
											"-->>" + hobby[which], 1).show();
								}
							}
						});
				/*
				 * 取消按钮
				 */
				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) {

							}
						});
				builder.show();

			}
		});
		/*
		 * 3.单选对话框的设定
		 */
		button3.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
				builder.setTitle("XX");
				builder.setSingleChoiceItems(new String[]{"上网", "打游戏", "聊天"}, -1, new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
				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) {
						
					}
				});
				builder.show();
			}
		});
	}

	/*
	 * private final View.OnClickListener mylistenerClickListener = new
	 * OnClickListener() {
	 * 
	 * @Override public void onClick(View v) { // TODO Auto-generated method
	 * stub
	 * 
	 * } };
	 */

	@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;
	}

}

(3)结果展示

技术分享

技术分享

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。