测试代码:

布局:

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. Js获取当前日期时间及时间相关操作

    Js获取当前日期时间及时间格式 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  2. (easy)LeetCode 219.Contains Duplicate II

    Given an array of integers and an integer k, find out whether there there are two distinct indices i ...

  3. Party Games

    Party Games You've been invited to a party. The host wants to divide the guests into 2 teams for par ...

  4. C语言程序设计50例(二)(经典收藏)

    [程序11]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月 后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析: 兔子的规律为数列1,1 ...

  5. 学习练习 java 小题

    Scanner a = new Scanner(System.in); System.out.println("请输入您的分数"); int fen = a.nextInt(); ...

  6. Facebook 的系统架构(转)

    来源:http://www.quora.com/What-is-Facebooks-architecture(由Micha?l Figuière回答) 根据我现有的阅读和谈话,我所理解的今天Faceb ...

  7. devexpress中如何绑定ASPxTreeList控件

    效果图: //前端控件代码: <dx:ASPxTreeList ID="Tree_Gooslist" AutoGenerateColumns="False" ...

  8. Duilib学习笔记《07》— 资源加载

    Duilib的界面表现力能如此丰富,很大程度上得益于贴图描述的简单强大.通过之前的学习及参看相关例子,我们可以发现,在XML布局文件中,不管是窗体背景还是控件,都添加了对应的图片资源以此来美化界面.而 ...

  9. ECSHOP如何解决Deprecated: preg_replace()报错 Home / 开源程序 / ECSHOP / ECSHOP如何解决Deprecated: preg_replace()报错

    Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in D:\w ...

  10. ansible 访问内网服务器

    ssh https://medium.com/@paulskarseth/ansible-bastion-host-proxycommand-e6946c945d30#.rauzlfv0z http: ...