Android控件——7种形式的Android Dialog使用举例(转载)
在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择。这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一下,Android Dialog的类型无非也就7种,下面我分别向大家介绍这7种Android Dialog对话框的使用方法,希望对大家能有所帮助。
1.该效果是当按返回按钮时弹出一个提示,来确保无误操作,采用常见的对话框样式。

创建dialog对话框方法代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
protected void dialog() { AlertDialog.Builder builder = new Builder(Main. this ); builder.setMessage( "确认退出吗?" ); builder.setTitle( "提示" ); builder.setPositiveButton( "确认" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Main. this .finish(); } }); builder.setNegativeButton( "取消" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } |
在onKeyDown(int keyCode, KeyEvent event)方法中调用此方法
|
1
2
3
4
5
6
|
public boolean onKeyDown( int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) { dialog(); } return false ; } |
2.改变了对话框的图表,添加了三个按钮

创建dialog的方法代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
Dialog dialog = new AlertDialog.Builder( this ).setIcon( android.R.drawable.btn_star).setTitle( "喜好调查" ).setMessage( "你喜欢李连杰的电影吗?" ).setPositiveButton( "很喜欢" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main. this , "我很喜欢他的电影。" , Toast.LENGTH_LONG).show(); } }).setNegativeButton( "不喜欢" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main. this , "我不喜欢他的电影。" , Toast.LENGTH_LONG) .show(); } }).setNeutralButton( "一般" , new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(Main. this , "谈不上喜欢不喜欢。" , Toast.LENGTH_LONG) .show(); } }).create(); dialog.show(); |
3.信息内容是一个简单的View类型

创建dialog方法的代码如下:
|
1
2
3
4
|
new AlertDialog.Builder( this ).setTitle( "请输入" ).setIcon( android.R.drawable.ic_dialog_info).setView( new EditText( this )).setPositiveButton( "确定" , null ) .setNegativeButton( "取消" , null ).show(); |
4.信息内容是一组单选框

创建dialog方法的代码如下:
|
1
2
3
4
|
new AlertDialog.Builder( this ).setTitle( "复选框" ).setMultiChoiceItems( new String[] { "Item1" , "Item2" }, null , null ) .setPositiveButton( "确定" , null ) .setNegativeButton( "取消" , null ).show(); |
5.信息内容是一组多选框

创建dialog方法的代码如下:
|
1
2
3
4
5
6
7
8
|
new AlertDialog.Builder( this ).setTitle( "单选框" ).setIcon( android.R.drawable.ic_dialog_info).setSingleChoiceItems( new String[] { "Item1" , "Item2" }, 0 , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).setNegativeButton( "取消" , null ).show(); |
6.信息内容是一组简单列表项

创建dialog的方法代码如下:
|
1
2
3
|
new AlertDialog.Builder( this ).setTitle( "列表框" ).setItems( new String[] { "Item1" , "Item2" }, null ).setNegativeButton( "确定" , null ).show(); |
7.信息内容是一个自定义的布局

dialog布局文件代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "utf-8" ?> android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:background = "#ffffffff" android:orientation = "horizontal" android:id = "@+id/dialog" > < TextView android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/tvname" android:text = "姓名:" /> < EditText android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:id = "@+id/etname" android:minWidth = "100dip" /> </ LinearLayout > |
创建dialog方法的代码如下:
|
1
2
3
4
5
6
|
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog, (ViewGroup) findViewById(R.id.dialog)); new AlertDialog.Builder( this ).setTitle( "自定义布局" ).setView(layout) .setPositiveButton( "确定" , null ) .setNegativeButton( "取消" , null ).show(); |
Android控件——7种形式的Android Dialog使用举例(转载)的更多相关文章
- Android控件点击事件
1. 介绍 本文介绍了Android控件的点击事件 Android控件点击(onClick)事件可以用如下三种方式来实现 2. 实现onClick方法 在layout的xml中指定onClick方法, ...
- Android控件系列之RadioButton&RadioGroup(转)
学习目的: 1.掌握在Android中如何建立RadioGroup和RadioButton 2.掌握RadioGroup的常用属性 3.理解RadioButton和CheckBox的区别 4.掌握Ra ...
- Android初级教程_获取Android控件的宽和高
转载:http://blog.csdn.net/johnny901114/article/details/7839512 我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看 ...
- Android入门(六):Android控件布局属性全解
第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中 (Hrizontal表示水平)android:layout_centerVertical 垂 ...
- Android 控件架构及View、ViewGroup的测量
附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...
- Android控件属性大全(转)
http://blog.csdn.net/pku_android/article/details/7365685 LinearLayout 线性布局 子元素任意: Tab ...
- UIAutomator定位Android控件的方法
UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...
- Android控件系列之CheckBox
学习目的: 1.掌握在Android中如何建立CheckBox 2.掌握CheckBox的常用属性 3.掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Bu ...
- android控件的属性
android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...
随机推荐
- Node.js meitulu图片批量下载爬虫1.051
原有1.05版程序没有断点续传模式,现在在最近程序基础上改写一版1.051. //====================================================== // m ...
- PHP上传文件类 代码练习
类文件: <?php class upload{ protected $fileName; protected $uploadPath; protected $maxSize; protecte ...
- App服务端架构变迁
随着移动互联网时代的到来,移动技术也随之飞速发展.如今,App已然成为绝大多数互联网企业用来获取用户的核心渠道.以往以PC为主要承载平台的各业务线,源源不断集成加入到移动项目中来,原本以产品为中心快速 ...
- UITableView Scroll to top 手动设置tableview 滚动到 顶部
UITableView Scroll to top 手动设置tableview 滚动到 顶部 [mainTableView scrollRectToVisible:CGRectMake(0,0,1,1 ...
- 饿了么ui添加事件
最近饿了么ui挺火,连美团都有项目组再用,刚好最近项目重构,就引入了进来,刚用上就发现一个大坑,在配合vue使用时,居然无法添加自定义事件 找了半天才发现原因是需要在事件后面加上 ‘’.native ...
- 在k8s上部署第一个php应用
一.搭建nginx+php 1.站点配置文件 1.1创建nginx-configmap.yaml [root@master k8s]# cat nginx-configmap.yaml apiVers ...
- BroadcastReceiver应用详解——广播
转自:http://blog.csdn.net/liuhe688/article/details/6955668 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收 ...
- Android自动化测试之MonkeyRunner录制和回放脚本
Android自动化测试之MonkeyRunner录制和回放脚本(十一) 分类: 自动化测试 Android自动化 2013-02-22 10:57 7346人阅读 评论(2) 收藏 举报 andro ...
- mongodb - Replication Set成员维护
1.添加复制集成员 ybb:PRIMARY> rs.add("cc.example.com:27020") { "ok" : 1 } ybb:PRIMAR ...
- python 脚本(获取指定文件夹、指定文件格式、的代码行数、注释行数)
1.代码的运行结果: 获取 指定文件夹下.指定文件格式 文件的: 总代码行数.总注释行数(需指定注释格式).总空行数: #coding: utf-8 import os, re # 代码所在目录 FI ...