对话框AlertDialog的基本类型与创建

测试代码:
布局:
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的基本类型与创建的更多相关文章
- 安卓弹出对话框——Alertdialog
在Android开发当中,在界面上弹出一个Dialog对话框使我们经常需要做的,本篇随笔将详细的讲解Dialog对话框这个概念,包括定义不同样式的对话框. 一.Dialog 我们首先来看看androi ...
- 【转】【Android】对话框 AlertDialog -- 不错不错
原文网址:http://blog.csdn.net/feng88724/article/details/6171450 本讲介绍一下Android基本组件:对话框AlertDialog. API: j ...
- Android中的对话框AlertDialog使用技巧合集-转载
Android中的对话框AlertDialog使用技巧合集 文章来自:http://blog.csdn.net/blue6626/article/details/6641105 今天我用自 ...
- 安卓弹出对话框——Alertdialog(一)
首先看各种样式的对话框: 我们看到,Dialog有很多的子类实现,所以我们要定义一个对话框,使用其子类来实例化一个即可,而不要直接使用Dialog这个父类来构造. 二.AlertDialog 今天我们 ...
- android中提示&对话框----AlertDialog
AlertDialog(对话框) 一.对话框的基本使用流程 step1:创建AlertDialog.Buider; step2:调用setIcon()设置图标,setTitle()或者setCusto ...
- 【Andriod-AlertDialog控件】 弹出对话框AlertDialog用法
Result: Code: import android.app.Activity; import android.app.AlertDialog; import android.content.Di ...
- 第26讲 对话框AlertDialog的自定义实现
第26讲对话框AlertDialog的自定义实现 比如我们在开发过长当中,要通过介绍系统发送的一个广播弹出一个dialog.但是dialog必需是基于activity才能呈现出来,如果没有activi ...
- iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结
iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...
- [UE4]使用C++重写蓝图,SpawnObject根据类型动态创建UObject
先大量使用蓝图制作项目,后续再用C++把复杂的蓝图重写一遍,用C++代码按照蓝图依葫芦画瓢就可以了,很简单,但需要遵守一些原则: 第一种方法:使用继承 一.创建一个C++类作为蓝图的父类(C++类继承 ...
随机推荐
- Oracle常用命令1
一. 安装是用户管理: sqlplus /nolog; connect /as sysdba; alter user sys identified by change_on_install; alte ...
- SQL SERVER系统表
sysaltfiles 主数据库 保存数据库的文件 syscharsets 主数据库 字符集与排序顺序sysconfigures 主数据库 配置选项syscurconfigs 主数据库 当前配置选项s ...
- Network of Schools(强连通分量缩点(邻接表&矩阵))
Description A number of schools are connected to a computer network. Agreements have been developed ...
- android 自定义组合控件 顶部导航栏
在软件开发过程中,经常见到,就是APP 的标题栏样式几乎都是一样的,只是文字不同而已,两边图标不同.为了减少重复代码,提高效率, 方便大家使用,我们把标题栏通过组合的方式定义成一个控件. 例下图: 点 ...
- git 仓库操作
一.git 仓库从远程clone 首先要建立一个本地空目录文件比如 RuntimeJsonModel,然后: 1. git init 2. git clone https://github.com/G ...
- Oracle 删表前验证表名是否存在并且删除
DECLARE num NUMBER; BEGIN SELECT COUNT(1) INTO num FROM USER_TABLES WHERE TABLE_NAME = UPPER('tableN ...
- typedef 与define 的区别
typedef和#define的用法与区别 typedef和#define的用法与区别 一.typedef的用法 在C/C++语言中,typedef常用来定义一个标识符及关键字的别名,它是语言编译 ...
- dll显式加载与隐式加载
使用动态DLL有两种方法,一种是隐式链接,一种是显式链接,如果用loadlibrary就是显示链接,用lib就属于隐式链接. 两种方法对于你的程序调用动态库时没有任何区别,只是你在编程时,步骤是不一样 ...
- oracle:触发器,自治事务 trigger
create or replace trigger TRI_FC83_INSERT before insert ON FC83 FOR EACH ROW declare PRAGMA AUTONOMO ...
- 《Unix/Linux日志分析与流量监控》书稿完成
<Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...