Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框

Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框

大家好,又见面了,我是全栈君。

AlertDialog能够生成各种内容的对话框。可是每种对话框都会有这样的的结构:

Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框

类似下边这样的的:

Adrnoid开发系列(二十五):使用AlertDialog创建各种类型的对话框

这仅仅是最简单的对话框。

我们来看下创建一个对话框须要的步骤:

1、使用创建AlertDialog.Builder对象

2、调用AlertDialog.Builder的setTitle()或setCustomTitle()方法设置标题

3、调用AlertDialog.Builder的setIcon()方法设置图标

4、调用一些其它设置方法设置标题

5、调用AlertDialog.Builder的setPositiveButton()、setNegativeButton()或者setNeutralButton()加入多个button

6、调用create()方法创建AlertDialog对象,再调用AlertDialog对象的show()方法将该对话框显示出来。

新建Android项目,然后编写main.xml:

<?

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal"><!-- 显示一个普通的文本编辑框组件 --><EditText android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false"/><!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单对话框" android:onClick="simple" /><!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="简单列表项对话框" android:onClick="simpleList" /> <!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="单选列表项对话框" android:onClick="singleChoice" /> <!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="多选列表项对话框" android:onClick="multiChoice" /> <!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自己定义列表项对话框" android:onClick="customList" /> <!-- 定义一个普通的button组件 --><Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自己定义View对话框" android:onClick="customView" /> </LinearLayout>

这里是定义了6个button和一个文本显示框。而且设置了对应的onClick属性

接下来,我们就要编写主界面的java代码:AlertDialogTest.java

package org.crazyit.ui;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.TableLayout;
import android.widget.TextView;


public class AlertDialogTest extends Activity
{
	TextView show;
	String[] items = new String[] { 
			"疯狂Java讲义", "疯狂Ajax讲义",
			"轻量级Java EE企业应用实战",
			"疯狂Android讲义" };	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		show = (TextView) findViewById(R.id.show);
	}

	public void simple(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			// 设置对话框标题
			.setTitle("这是对话框标题")
			// 设置图标
			.setIcon(R.drawable.tools)
			.setMessage("这是对话框内容");
		// 为AlertDialog.Builder加入【确定】button
		setPositiveButton(builder);
		// 为AlertDialog.Builder加入【取消】button
		setNegativeButton(builder)
			.create()
			.show();
	}

	public void simpleList(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			// 设置对话框标题
			.setTitle("简单列表项对话框")
			// 设置图标
			.setIcon(R.drawable.tools)
			// 设置简单的列表项内容
			.setItems(items, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					show.setText("你选中了《" + items[which] + "》");
				}
			});
		// 为AlertDialog.Builder加入【确定】button
		setPositiveButton(builder);
		// 为AlertDialog.Builder加入【取消】button
		setNegativeButton(builder)
			.create()
			.show();
	}

	public void singleChoice(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			// 设置对话框标题
			.setTitle("单选列表项对话框")
			// 设置图标
			.setIcon(R.drawable.tools)
			// 设置单选列表项,默认选中第二项(索引为1)
			.setSingleChoiceItems(items, 1, new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					show.setText("你选中了《" + items[which] + "》");
				}
			});
		// 为AlertDialog.Builder加入【确定】button
		setPositiveButton(builder);
		// 为AlertDialog.Builder加入【取消】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	public void multiChoice(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			// 设置对话框标题
			.setTitle("多选列表项对话框")
			// 设置图标
			.setIcon(R.drawable.tools)
			// 设置多选列表项,设置勾选第2项、第4项
			.setMultiChoiceItems(items
			, new boolean[]{false , true ,false ,true}, null);
		// 为AlertDialog.Builder加入【确定】button
		setPositiveButton(builder);
		// 为AlertDialog.Builder加入【取消】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	public void customList(View source)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this)
			// 设置对话框标题
			.setTitle("自己定义列表项对话框")
			// 设置图标
			.setIcon(R.drawable.tools)
			// 设置自己定义列表项
			.setAdapter(new ArrayAdapter<String>(this 
					, R.layout.array_item 
					, items), null);
		// 为AlertDialog.Builder加入【确定】button
		setPositiveButton(builder);
		// 为AlertDialog.Builder加入【取消】button
		setNegativeButton(builder)
			.create()
			.show();
	}
	
	public void customView(View source)
	{
		//装载/res/layout/login.xml界面布局
		TableLayout loginForm = (TableLayout)getLayoutInflater()
			.inflate( R.layout.login, null);		
		new AlertDialog.Builder(this)
			// 设置对话框的图标
			.setIcon(R.drawable.tools)
			// 设置对话框的标题
			.setTitle("自己定义View对话框")
			// 设置对话框显示的View对象
			.setView(loginForm)
			// 为对话框设置一个“确定”button
			.setPositiveButton("登录" , new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog,
						int which)
				{
					// 此处可运行登录处理
				}
			})
			// 为对话框设置一个“取消”button
			.setNegativeButton("取消", new OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog,
						int which)
				{
					// 取消登录。不做不论什么事情。

} }) // 创建、并显示对话框 .create() .show(); } private AlertDialog.Builder setPositiveButton( AlertDialog.Builder builder) { // 调用setPositiveButton方法加入确定button return builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了【确定】button!

"); } }); } private AlertDialog.Builder setNegativeButton( AlertDialog.Builder builder) { // 调用setNegativeButton方法加入取消button return builder.setNegativeButton("取消", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { show.setText("单击了【取消】button!"); } }); }}

在这里边,第五个和第六个button用到了两个样式:array_item.xml和login.xml

我们看下他们的内容:

array_item.xml:

<?

xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:textColor="#f0f" android:textSize="30dp" android:shadowColor="#ff0" android:shadowRadius="2" android:shadowDx="5" android:shadowDy="5" android:layout_width="match_parent" android:layout_height="wrap_content" />

login.xml:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/loginForm"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="username:"
	android:textSize="10pt"
	/>
<!-- 输入username的文本框 -->
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint="请填写登录帐号"
	android:selectAllOnFocus="true"
	/>
</TableRow>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="password:"
	android:textSize="10pt"	
	/>
<!-- 输入password的文本框 -->	
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint="请填写password"	
	android:password="true"
	/>
</TableRow>
<TableRow>
<TextView
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="电话号码:"
	android:textSize="10pt"	
	/>
<!-- 输入电话号码的文本框 -->		
<EditText
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:hint="请填写您的电话号码"
	android:selectAllOnFocus="true"
	android:phoneNumber="true"
	/>
</TableRow>
</TableLayout>

通过AlertDialog能够制作出不同风格的对话框,在非常多时候都比較实用

而且我们能够通过确定button来把数据通过Intent传递到另外一个界面中。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116202.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 空间相关分析(三) 局部莫兰指数的理解与计算「建议收藏」

    空间相关分析(三) 局部莫兰指数的理解与计算「建议收藏」        在上篇中,我们详细地阐述了全局莫兰指数(GlobalMoran’I)的含义以及具体的软件实操方法。今天,就来进一步地说明局部莫兰指数(LocalMoran’I)的含义与计算。        首先说明一下进行局部相关分析的必要性:在全局相关分析中,如果全局莫兰指数显著,我们即可认为在该区域上存在空间相关性。但是,我们还是不知道

    2022年6月25日
    106
  • 大数据经典案例有哪些?

    大数据经典案例有哪些?“互联网还没搞清楚的时候,移动互联就来了移动互联还没搞清楚的时候,大数据就来了”。近两年,“大数据”这个词越来越为大众所熟悉,“大数据”一直是以高冷的形象出现在大众面前,面对大数据,相信许多人都一头雾水。下面我们通过几个经典案例,让大家实打实触摸一把“大数据”。你会发现它其实就在身边而且也是很有趣的。1.啤酒与尿布全球零售业巨头沃尔玛在对消费者购物行为分析时发现,男性顾客在购买婴儿尿片时,常常会顺便搭配几瓶啤酒来犒劳自己,于是尝试推出了将啤酒和尿布摆在一起的促销手段。没想到这个举措居然使尿布

    2022年5月2日
    47
  • tuple object is not callable解决方案[通俗易懂]

    tuple object is not callable解决方案[通俗易懂]在按照书上的代码操作的时候,有些时候会遇到一些很奇怪的bug,标题就是一个这样的bug。操作实例的时候是用了shape函数为了解决这个bug,查了很多资料,都没有找到解决方案,最后不断尝试,并结合了一点经验解决了。解决之后发现问题也特别简单在python中,只有函数才是Callable(可Call的对象才是Callable)。但是tuple是一个数据类型,当然是不能Call(翻译成:使唤

    2025年8月8日
    4
  • 更新日志.txt

    【20210811.1905.3】增加了几处新功能:1.单码登录功能2.账号与用户电脑机械码绑定<url>https://zhuanlan.zhihu.com/p/350520546</url>【20210811.1905.2】优化了几个子程序算法,提高运行速度【20210811.1905.1】1.更新了xxx内容2.修复了几处小bug….

    2022年4月7日
    46
  • java trylock_lock.tryLock()方法的使用

    java trylock_lock.tryLock()方法的使用packageconcurrent;importjava.util.ArrayList;importjava.util.List;importjava.util.concurrent.locks.Lock;importjava.util.concurrent.locks.ReentrantLock;publicclassTestTryLock{privateListlist=…

    2022年10月16日
    4
  • BoundsChecker安装下载及使用教程攻略

    BoundsChecker安装下载地址:参见文章结尾附件1前言我在本文中详细介绍了测试工具NuMegaDevpartner(以下简称NuMega)的使用方法。NuMega是一个动态测试工具,主要应用于白盒测试。该工具的特点是学习简单、使用方便、功能有效。NuMega共有三个独立的子功能——BoundsChecker、TrueCoverage、TrueTime。BoundsChecker

    2022年4月6日
    54

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号