Android之AlertDialog详解

1,对话框是当前界面弹出的一个小窗口,用于显示重要提示信息,提示用户输入信息,确认信息,或者显示某种状态,如下载进度,退出提示等等。一般情况下,用户要和对话框进行交互,然后返回到被遮盖的界面以继续当前的应用程序。
2,AlertDialog常用方法:
a)要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。
b)setTitle:为对话框设置标题。
c)setIcon:为对话框设置图标。
d)setMessage:为对话框设置内容。
e)setView:为对话话框设置自定义样式。
f ) setItems:设置对话框要显示的一个list,一般用于显示几个命令时。
g)setMultiChoiceItems:设置对话框显示一系列的复选框。
h)setSingleChoiceItems:设置单选按钮。
i)setNeutralButton:设置普通按钮。
j)setPositiveButton:给对话框添加“确定”按钮。

k)setNegativeButton:给对话框添加“取消”按钮。

l)setCancelable:设置对话框是否可以取消。

m)setCanceledOnTouchOutside:设置点击对话框外面是否取消对话框。

3,下面通过一个实例来介绍五种常见的对话框,分别是确认对话框、单选按钮对话框、多选按钮对话框、列表

对话框和自定义对话框。下面是五个按钮的布局文件:

<span style="font-size:18px;"><pre name="code" class="html"><span style="font-size:18px;"><LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/bt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="普通对话框" />
    <Button
        android:id="@+id/bt2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="单选按钮对话框" />
    <Button
        android:id="@+id/bt3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="多选按钮对话框" />
    <Button
        android:id="@+id/bt4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="列表对话框" />
    <Button
        android:id="@+id/bt5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框" />

</LinearLayout></span></span>

 

4,自定义对话框的布局文件(dialog_layout.xml):

<span style="font-size:18px;"><span style="font-size:18px;"><?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" >
    
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:hint="输入内容......" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="提交"
        android:layout_weight="1"/>
</LinearLayout>

<ImageView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:src="@drawable/camera"/>
</LinearLayout></span></span>

5,在MainActivity类中设置各个按钮的对话框:

<span style="font-size:18px;"><span style="font-size:18px;">package com.example.alertdialog2;

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.Toast;

public class MainActivity extends Activity {

	String[] single_list={"男","女"}; //用于单选按钮
	String[] multi_list={"篮球","爬山","游泳","乒乓球","听歌"}; //用于多选按钮
	String[] item_list={"项目经理","策划","美工","程序猿"}; //用于列表按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initEven();
    }
    /*
     * 初始化事件
     */
	//初始化第一个按钮
	private void initEven() {
		findViewById(R.id.bt1).setOnClickListener(new OnClickListener() {
		
			@Override
			public void onClick(View arg0) {
				//showDialog这个方法是在外部定义的,把这个方法引入到点击事件中
				showDialog1();
			}
		}); 
		//初始化第二个按钮
		findViewById(R.id.bt2).setOnClickListener(new OnClickListener() {
		
		
		@Override
		public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog2();
			}
		});
		// 初始化第三个按钮
		findViewById(R.id.bt3).setOnClickListener(new OnClickListener() {
		
		
		@Override
		public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog3();
			}
		});
		// 初始化第四个按钮
		findViewById(R.id.bt4).setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog4();
			}
		});
		//初始化第五个按钮
		findViewById(R.id.bt5).setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showDialog5();
			}
		});
	}
	
	/*
	* 显示确认按钮对话框
	*/
	public void showDialog1(){
	AlertDialog.Builder builder=new AlertDialog.Builder(this);
	builder.setTitle("确认对话框");
	builder.setIcon(R.drawable.ic_launcher);
	builder.setMessage("确认对话框提示内容");
	/*这个OnClickListener引用的包为:android.content.DialogInterface.OnClickListener.
	*而上面的OnClickListener引用的包为:android.view.View.OnClickListener.
	*为了区分开,需要加在这个前面加上DialogInterface.
	*/
	builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
	
	@Override
	public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(MainActivity.this, "点击了确定按钮!", Toast.LENGTH_SHORT).show();
		}
	});
	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
	
	@Override
	public void onClick(DialogInterface arg0, int arg1) {
			// TODO Auto-generated method stub
			Toast.makeText(MainActivity.this, "点击了取消按钮!", Toast.LENGTH_SHORT).show();
		}
	});
	//获取dialog
	AlertDialog dialog=builder.create();
	//显示对话框
	dialog.show();
	}
	/*
	*显示单选按钮对话框
	*/
	public void showDialog2(){
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		builder.setTitle("选择性别");
		builder.setIcon(R.drawable.ic_launcher);
		// 单选按钮有三个参数,第一个数据源,第二个是当前选中的,第三个是监听事件
		builder.setSingleChoiceItems(single_list, 0, new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface arg0, int which) {
			String str=single_list[which];
			Toast.makeText(MainActivity.this, "性别是:"+str, Toast.LENGTH_SHORT).show();
		}
	});
	
	//获取dialog
	AlertDialog dialog=builder.create();
		//显示对话框
		dialog.show();
	} 
	/*
	*显示多选按钮对话框
	*/
	public void showDialog3() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("选择爱好");
		builder.setIcon(R.drawable.ic_launcher);
		// 多选按钮有三个参数,第一个数据源,第二个是当前选中的(可以多个),第三个是监听事件
		builder.setMultiChoiceItems(multi_list, null,
		new DialogInterface.OnMultiChoiceClickListener() {
		
		
		@Override
		public void onClick(DialogInterface arg0, int which,
		boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			Toast.makeText(MainActivity.this,
			"我的爱好是:" + multi_list[which],
			Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(MainActivity.this,
			"我不喜欢:" + multi_list[which],
			Toast.LENGTH_SHORT).show();
			}
		}
	});
	//隐藏对话框
	builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
	
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
		});
		AlertDialog dialog = builder.create();
		dialog.show();
		}
		/*
		*显示列表话框
		*/
		public void showDialog4(){
		AlertDialog.Builder builder=new AlertDialog.Builder(this);
		builder.setTitle("选择职位");
		builder.setIcon(R.drawable.ic_launcher);
		builder.setItems(item_list, new DialogInterface.OnClickListener() {
		
		@Override
		public void onClick(DialogInterface arg0, int which) {
			Toast.makeText(MainActivity.this,
			"我是:" + item_list[which],
			Toast.LENGTH_SHORT).show();
		}
		});
		AlertDialog dialog=builder.create();
		dialog.show();
	}
	/*
	*显示自定义按钮对话框
	*/
	public void showDialog5(){
	//通过inflate来获取自定义的视图
	LayoutInflater inflater=LayoutInflater.from(this);
	View view=inflater.inflate(R.layout.dialog_layout, null);
	AlertDialog.Builder builder=new AlertDialog.Builder(this);
	builder.setTitle("搜索相册");
	builder.setIcon(R.drawable.ic_launcher);
	//通过setView方法把自定义的视图添加到builder
	builder.setView(view);
	//获取dialog
	AlertDialog dialog=builder.create();
	//显示对话框
	dialog.show();
	}

}</span></span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值