测试代码:

布局:

activity_main.xml:

 <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.zzw.testalerdialog.MainActivity" > <Button
android:id="@+id/horizontally"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="横向显示" /> <Button
android:id="@+id/vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="竖向显示" /> <Button
android:id="@+id/singleChoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="单选框显示" /> <Button
android:id="@+id/multiChoice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="多选框显示" /> <Button
android:id="@+id/myDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="20dp"
android:text="自定义显示" /> <ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:src="@drawable/ic_launcher" /> </LinearLayout>

JAVA代码:

 package com.zzw.testalerdialog;

 import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity implements View.OnClickListener { private Context context; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); init();
} private void init() { context = this; findViewById(R.id.horizontally).setOnClickListener(this);
findViewById(R.id.vertical).setOnClickListener(this);
findViewById(R.id.singleChoice).setOnClickListener(this);
findViewById(R.id.multiChoice).setOnClickListener(this);
findViewById(R.id.myDialog).setOnClickListener(this); } // 横向类型显示
private void horizontallyShow() { AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("横向显示");
dialog.setMessage("提示信息");
// DialogInterface.BUTTON_POSITIVE作用是显示的顺序
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "确定", 0).show();
}
}); dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "取消", 0).show();
}
}); dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "考虑", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "考虑", 0).show();
}
}); dialog.show();
} // 竖向类型显示
private void verticalShow() { final String[] items = new String[3];
for (int i = 0; i < 3; i++) {
items[i] = "选项--" + i;
} AlertDialog dialog = new AlertDialog.Builder(context).setItems(items, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
}
}).create(); dialog.setTitle("竖向显示");
dialog.setIcon(R.drawable.ic_launcher); dialog.show(); } // 单选框类型显示
private void singleChoiceShow() { final String[] items = new String[3];
for (int i = 0; i < 3; i++) {
items[i] = "选项--" + i;
} AlertDialog.Builder mBuilder = new AlertDialog.Builder(context);
// mBuilder.setIcon(R.drawable.ic_launcher);
// mBuilder.setTitle("单选框显示"); // checkedItem默认选择的位置参数
mBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss(); }
}); AlertDialog dialog = mBuilder.create();
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("单选框显示");
dialog.show(); } // 复选框类型显示
private void multiChoiceShow() { final String[] items = new String[3];
for (int i = 0; i < 3; i++) {
items[i] = "选项--" + i;
} AlertDialog.Builder mBuilder = new AlertDialog.Builder(context); // checkedItems为默认勾选的状态
final boolean[] checkedItems = { false, true, false };
// 这个监听的作用是用于检测item选中状态的变化
mBuilder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) { }
}); AlertDialog dialog = mBuilder.create();
dialog.setIcon(R.drawable.ic_launcher);
dialog.setTitle("复选框显示"); // 用于确定,知道用户勾选了那些选项
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { String s = "";
for (int i = 0; i < items.length; i++) {
if (checkedItems[i])
s += items[i] + "\n";
}
Toast.makeText(context, s, Toast.LENGTH_SHORT).show();
}
}); // 用于取消
dialog.setButton(DialogInterface.BUTTON_NEUTRAL, "取消", new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { }
}); dialog.show();
} //自定义类型显示
private void myDialogShow(){ AlertDialog.Builder mBuilder = new AlertDialog.Builder(context); mBuilder.setView(getLayoutInflater().inflate(R.layout.my_dialog, null)); AlertDialog dialog=mBuilder.create();
// dialog.setIcon(R.drawable.ic_launcher);
// dialog.setTitle("自定义的对话框");
dialog.show();
} @Override
public void onClick(View v) { switch (v.getId()) {
case R.id.horizontally: horizontallyShow();
break;
case R.id.vertical: verticalShow();
break;
case R.id.singleChoice: singleChoiceShow();
break;
case R.id.multiChoice: multiChoiceShow();
break;
case R.id.myDialog:
myDialogShow();
break; }
} }

自定义的布局my_dialog.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#80CBC4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_launcher" /> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="www.cnblogs.com/zzw1994"
android:textColor="@android:color/holo_red_light" /> </LinearLayout>

对话框AlertDialog的基本类型与创建的更多相关文章

  1. 安卓弹出对话框——Alertdialog

    在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...

  2. 【转】【Android】对话框 AlertDialog -- 不错不错

    原文网址:http://blog.csdn.net/feng88724/article/details/6171450 本讲介绍一下Android基本组件:对话框AlertDialog. API: j ...

  3. Android中的对话框AlertDialog使用技巧合集-转载

    Android中的对话框AlertDialog使用技巧合集     文章来自:http://blog.csdn.net/blue6626/article/details/6641105   今天我用自 ...

  4. 安卓弹出对话框——Alertdialog(一)

    首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...

  5. android中提示&对话框----AlertDialog

    AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...

  6. 【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法

    Result: Code: import android.app.Activity; import android.app.AlertDialog; import android.content.Di ...

  7. 第26讲 对话框AlertDialog的自定义实现

    第26讲对话框AlertDialog的自定义实现 比如我们在开发过长当中,要通过介绍系统发送的一个广播弹出一个dialog.但是dialog必需是基于activity才能呈现出来,如果没有activi ...

  8. iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结

    iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...

  9. [UE4]使用C++重写蓝图,SpawnObject根据类型动态创建UObject

    先大量使用蓝图制作项目,后续再用C++把复杂的蓝图重写一遍,用C++代码按照蓝图依葫芦画瓢就可以了,很简单,但需要遵守一些原则: 第一种方法:使用继承 一.创建一个C++类作为蓝图的父类(C++类继承 ...

随机推荐

  1. oracle登陆的命令是什么?导出数据表的命令是什么?

    登陆的命令: su - oracle sqlplus / as sysdba 导出数据表的命令: exp user/pwd@SID file=path/sth.dmp tables=(table1,t ...

  2. Cocos2dx老版本适配64位

    1.出现"__curl_rule_01__ "错误 解决方法: 下载新的第三方libcurl库.替换掉libcurl相关的库和头文件,libcul.a  .h文件. 相关路径:co ...

  3. KVM虚拟化(一)—— 介绍与简单使用

    一.架构及介绍 KVM(Kernel-based Virtual Machine)它由 Quramnet 开发,该公司于 2008年被 Red Hat 收购: 自Linux 2.6.20后整合到内核, ...

  4. c# 控制服务启动停止

    public string StartService(string serviceName, bool serviceFlag) { try { using (System.ServiceProces ...

  5. CentOS安装 Docker

    系统的要求64 位操作系统,内核版本至少为 3.10. Docker 目前支持 CentOS 6.5 及以后的版本,推荐使用 CentOS 7 系统. cat /proc/version 首先,也是要 ...

  6. Oracle之别名小结

    今天在写一个简单的SQL语句并执行时抛出了如下图所示的一个错误提示信息! 恩,此异常信息很明显,在SQL语句中标示符的长度过长了,简短一些就应该没问题了,我查看了一下我的SQL语句发现是我的查询字段的 ...

  7. php 数组转json格式

    1.php若为关系数组:转化为由花括号包围的对象: 输入:$test = array("1"=>1,"2"=>2,"3"=> ...

  8. No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  9. NULL值比较,两个列的合并,列值按条件替换。

    show create table 表名 -- 显示创建表的sql语句. 为已有的表增加新列.alter table 表名 add 列名 int NULL -- 此行加了一个int 类型 默认可以nu ...

  10. 简单的TCPIP 客户端 服务器

    // soClient.cpp : Defines the entry point for the console application. // #include "stdafx.h&qu ...