3.五种常见对话框

本文介绍了Android开发中的五种常见对话框:普通对话框、单选对话框、多选对话框、进度条对话框和消息对话框。普通对话框用于提示信息;单选对话框和多选对话框分别用于单选和多选操作;进度条对话框适用于耗时任务展示;消息对话框则是轻量级的信息提醒机制。

3.五种常见对话框

简介

对话框也是程序与用户交互的一种方式,通常用于显示当前程序提示信息以及相关说明,以小窗口形式展现。

常见对话框有以下几种:

  • 普通对话框
  • 单选对话框
  • 多选对话框
  • 进度条对话框
  • 消息对话框
  • 自定义对话框

本文主要介绍前五种对话框,对于自定义对话框暂不赘述。

一、普通对话框

普通对话框(Dialog)一般只会显示提示信息,并具有确定和取消按钮。

在这里插入图片描述

private void btn_normalClcik() {
		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("警告");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 提示信息
		builder.setMessage("你确定要退出吗?");

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了确定按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 一定要把对话框显示出来
		builder.show();

	}

二、单选对话框

单选对话框和RadioButton作用类似,只能选择一个选项,它是通过AlertDialog对象调用setSingleChoiceItems()方法创建的。

在这里插入图片描述

private void btn_singleChoseCLick() {

		// 创建默认值
		int moren = 1;

		checked = moren;

		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("请选择你的性别?");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 创建items
		final String[] items = new String[] { "男", "女", "中" };

		// 设置内容 -1 表示没有默认值
		builder.setSingleChoiceItems(items, moren,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// System.out.println("----which---"+which);

						checked = which;
					}
				});

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			// which -2
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

				Toast.makeText(getApplicationContext(), items[checked],
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			// which -1
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 显示出来
		builder.show();

	}

三、多选对话框

多选对话框通常在需要勾选多种选项时使用,例如添加兴趣爱好、喜爱的电影等。创建多选对话框与创建单选对话框类似,调用setMultiChoiceItems()方法就可实现。

在这里插入图片描述

	// 点击多选对话框
	private void btn_moreChoseCLick() {
		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("请选择你喜欢的运动?");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 创建items
		final String[] items = new String[]{"乒乓球","羽毛球","篮球","足球"};
		
		//创建默认值 
		final boolean[] checkedItems = new boolean[]{false,false,true,false};
		// 设置内容 
		builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
//				System.out.println("which = "+which + ", ischecked :"+isChecked );
				checkedItems[which] = isChecked;	
			}
		});
		

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			// which -2
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				//代码实现
				String text ="";
				for (int i = 0; i < items.length; i++) {
					if(checkedItems[i]){
						text+=items[i];
					}
				}

				Toast.makeText(getApplicationContext(), "你喜欢的运动是"+text,
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			// which -1
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 显示出来
		builder.show();

	}

四、进度条对话框

进度条对话框一般在应用程序实现耗时操作时使用。Android中提供了两种进度条样式,圆形进度条和水平进度条。

在这里插入图片描述

	//进度条对话框
	private void btn_progresCLick() {
		
		final ProgressDialog pro = new ProgressDialog(this);
		
		pro.setIcon(R.drawable.ic_launcher);
		
		pro.setTitle("正在努力下载中》》》》");
		
		pro.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		
		
		//进度条最大值
		pro.setMax(100);
		//当前值
		
		new Thread(new Runnable() {	
			@Override
			public void run() {
				int progress =0;
				// TODO Auto-generated method stub
				for (int i = 1; i <= 100; i++) {
					
					pro.setProgress(i);
					//让线程睡一会
					try {
						new Thread().sleep(50);
						progress++;
						pro.incrementProgressBy(progress);
					}
					 catch (InterruptedException e) {
						e.printStackTrace();
					}	
				}
				
				//让对话框消失  
				pro.dismiss();
			}
		}).start();
		
		//对话框 消失的时候 应提示用户 下载完成 
		
		//代码实现

		pro.setOnDismissListener(new DialogInterface.OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface dialog) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),"下载完成",Toast.LENGTH_SHORT).show(); 
			}
		});
		
		
		
		
		pro.show();
	
		
	}

五、消息对话框

消息对话框(Toast)是轻量级信息提醒机制,显示在应用程序界面的最上层,一段时间后自动消失不会打断当前操作,也不获得焦点。
在这里插入图片描述

Toast.makeText ( this, "Hello,Toast" , Toast.LENGTH_SHORT ).show();

六、代码

layout文件:

<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="com.example.dialog.MainActivity" >

    <Button
        android:id="@+id/btn_normal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="普通对话框" />
    
    <Button
        android:id="@+id/btn_singlechose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="单选对话框" />
    
    <Button
        android:id="@+id/btn_morechose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="多选对话框" />
    
     <Button
        android:id="@+id/btn_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="进度条对话框" />

</LinearLayout>

MainActivity.java文件

package com.example.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources.Theme;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

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

		// 1.找到按钮
		Button btn_normal = (Button) findViewById(R.id.btn_normal);
		Button btn_singlechose = (Button) findViewById(R.id.btn_singlechose);
		Button btn_morechose = (Button) findViewById(R.id.btn_morechose);
		Button btn_progress = (Button) findViewById(R.id.btn_progress);

		// 2.监听按钮点击
		btn_normal.setOnClickListener(this);
		btn_singlechose.setOnClickListener(this);
		btn_morechose.setOnClickListener(this);
		btn_progress.setOnClickListener(this);
	}

	// 关于对话框 AlertDialog.Builder(this);
	// 获取上下文环境 :
	// 一定要用this 不能使用getApplicationContext()

	@Override
	// 按钮点击方法
	public void onClick(View v) {
		int id = v.getId();

		switch (id) {
		case R.id.btn_normal: // 点击的普通对话框按钮

			btn_normalClcik();

			break;
		case R.id.btn_singlechose: // 点击的单选对话框
			btn_singleChoseCLick();
			break;
		case R.id.btn_morechose: // 点击的多选对话框
			btn_moreChoseCLick();
			break;
			
		case R.id.btn_progress: // 点击的进度条对话框
			btn_progresCLick();
			break;

		}

	}

	//进度条对话框
	private void btn_progresCLick() {
		
		final ProgressDialog pro = new ProgressDialog(this);
		
		pro.setIcon(R.drawable.ic_launcher);
		
		pro.setTitle("正在努力下载中》》》》");
		
		pro.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		
		
		//进度条最大值
		pro.setMax(100);
		//当前值
		
		new Thread(new Runnable() {	
			@Override
			public void run() {
				int progress =0;
				// TODO Auto-generated method stub
				for (int i = 1; i <= 100; i++) {
					
					pro.setProgress(i);
					//让线程睡一会
					try {
						new Thread().sleep(50);
						progress++;
						pro.incrementProgressBy(progress);
					}
					 catch (InterruptedException e) {
						e.printStackTrace();
					}	
				}
				
				//让对话框消失  
				pro.dismiss();
			}
		}).start();
		
		//对话框 消失的时候 应提示用户 下载完成 
		
		//代码实现

		pro.setOnDismissListener(new DialogInterface.OnDismissListener() {
			
			@Override
			public void onDismiss(DialogInterface dialog) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(),"下载完成",Toast.LENGTH_SHORT).show(); 
			}
		});
		
		
		
		
		pro.show();
	
		
	}

	// 点击多选对话框
	private void btn_moreChoseCLick() {
		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("请选择你喜欢的运动?");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 创建items
		final String[] items = new String[]{"乒乓球","羽毛球","篮球","足球"};
		
		//创建默认值 
		final boolean[] checkedItems = new boolean[]{false,false,true,false};
		// 设置内容 
		builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				// TODO Auto-generated method stub
//				System.out.println("which = "+which + ", ischecked :"+isChecked );
				checkedItems[which] = isChecked;	
			}
		});
		

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			// which -2
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				//代码实现
				String text ="";
				for (int i = 0; i < items.length; i++) {
					if(checkedItems[i]){
						text+=items[i];
					}
				}

				Toast.makeText(getApplicationContext(), "你喜欢的运动是"+text,
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			// which -1
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 显示出来
		builder.show();

	}

	int checked = 2;

	// 单选对话框
	private void btn_singleChoseCLick() {

		// 创建默认值
		int moren = 1;

		checked = moren;

		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("请选择你的性别?");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 创建items
		final String[] items = new String[] { "男", "女", "中" };

		// 设置内容 -1 表示没有默认值
		builder.setSingleChoiceItems(items, moren,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// System.out.println("----which---"+which);

						checked = which;
					}
				});

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			// which -2
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

				Toast.makeText(getApplicationContext(), items[checked],
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			// which -1
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 显示出来
		builder.show();

	}

	// 点击了普通对话框
	private void btn_normalClcik() {
		// 1. 先要创建构建器 this
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		// 标题
		builder.setTitle("警告");

		// 图标
		builder.setIcon(R.drawable.ic_launcher);

		// 提示信息
		builder.setMessage("你确定要退出吗?");

		// 确定按钮
		// DialogInterface: 区分view包下的 OnClickListener();
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了确定按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

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

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(getApplicationContext(), "点击了取消按钮",
						Toast.LENGTH_SHORT).show();
			}
		});

		// 一定要把对话框显示出来
		builder.show();

	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DMr.Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值