对话框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++类继承 ...
随机推荐
- 【摘】linux之shutdown、halt和reboot命令详解
在重新启动Linux系统的同时把内存中的信息写入硬盘,应使用()命令实现 #shutdown -r now #halt #reboot #init3 正确答案:A 在linux命令中reboot是 ...
- XMLHttpRequest的五步使用方法
<html> <head> <title>Demo</title> <style> body,input,button,select,h1{ ...
- <%%>与<%#%>与<%=%>
在asp.net中经常出现包含这种形式<%%>的html代码,总的来说包含下面这样几种格式: 一. <%%> 这种格式实际上就是和asp的用法一样的,只是asp中里面是vbsc ...
- 聚类结果的评估指标及其JAVA实现
一. 前言 又GET了一项技能.在做聚类算法的时候,由于要评估所提出的聚类算法的好坏,于是需要与一些已知的算法对比,或者用一些人工标注的标签来比较,于是用到了聚类结果的评估指标.我了解了以下几项. 首 ...
- OC对象中的getter方法中不能用self.
@interface boy:NSObject { int _age; } - (void)setAge:(int)age; - (int)age; @end @implementation boy ...
- (旧)子数涵数·PS——水杯抠图
一.首先老规矩,下载所需要的素材. 二.打开Photoshop,并打开已下载好的素材. 三.使用"钢笔工具",快捷键为P,采用"路径"模式,将水杯抠出(例图左上 ...
- win7下Oracle 11的安装
把下载的win32_11gR2_database_1of2.zip和win32_11gR2_database_2of2.zip解压到一个database文件夹下,运行安装文件 Oracle11的卸 ...
- 【PL/SQL练习】控制结构
1.if判断: if-then-end if: SQL> declare v_ename emp.ename%type; v_sal emp.sal%type; begin select ena ...
- mysql和oracle的mybatis操作
1.Oracle.MySQL插入时返回下一个主键的操作 Oracle:<insert id="insert" parameterClass="ROLE"& ...
- [Oracle] 中的Temporary tablespace的作用
临时表空间主要用途是在数据库进行排序运算[如创建索引.order by及group by.distinct.union/intersect/minus/.sort-merge及join.analyze ...