Android简单例子——AlertDialog
最近学习了一段时间的Android,在网上找了些小的Demo,自己模拟这做了下,首先谢谢那些提供例子的朋友
今天主要学习的是简单的Dialog的使用(实现退出对话框)和自定义对话框
1.实现退出对话框

普通模式的对话框使用比较简单,主要是设置对话框标题、设置对话框内容、设置对话框中的按钮,以及增加监听事件,主要代码如下
//普通样式的对话框
btn2 = (Button) findViewById(R.id.btn2);
btn2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
/**声明一个Builder对象在当前Activity**/
AlertDialog.Builder ad = new Builder(MainActivity.this) ;
/**设置标题**/
ad.setTitle("退出应用");
/**设置主题内容**/
ad.setMessage("是否退出应用?");
/**设置按钮**/
ad.setPositiveButton("确认", new DialogInterface.OnClickListener() {
/**按钮增加监听时间,关闭activity**/
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
System.exit(0);
} }
);
/**设置按钮**/
ad.setNegativeButton("取消", new DialogInterface.OnClickListener() {
/**设置取消监听时间,关闭当前dilaog**/
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.create().show();
}
});
2.自定义对话框

自定义对话框相对来说使用就复杂了,主要是为自定义的对话框设置样式,在学习这个内容的时候,学会了selector的使用
Android中的Selector主要是用来改变ListView和Button控件的默认背景
例如在这个demo中定义的两个按钮样式
1.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--
1.android:state_focused:当控件获得焦点的时候
2.android:state_pressed:当控件按下的时候
3.android:state_selected:当控件选中的时候 -->
<item android:drawable="@drawable/linkbtnbgedim" android:state_pressed="true"/>
<item android:drawable="@drawable/linkbtnbg"/> </selector>
2.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/imgcanceled" android:state_pressed="true"/>
<item android:drawable="@drawable/imgcancel"/> </selector>
自定义对话框的样式代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <!-- 顶部,使用相对布局,更容易方便才做 -->
<RelativeLayout
android:id="@+id/customviewlayTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#1A94F9" > <TextView
android:id="@+id/customviewtvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:text="关于我们"
android:textColor="#000000" /> <ImageButton
android:id="@+id/customviewtvimgCancel"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/canceltor" />
</RelativeLayout> <!-- 中间内容 -->
<LinearLayout
android:id="@+id/customviewlayMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="@id/customviewlayTitle"
android:padding="20dp" > <TextView
android:id="@+id/orthertv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="作者:maozhanlei"
android:textColor="#000000" /> <TextView
android:id="@+id/orthertv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:text="博客:http://www.cnblogs.com/qadada/"
android:textColor="#000000" /> <TextView
android:id="@+id/orthertv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:text="Email:mzl0819@gmail.com"
android:textColor="#000000" />
</LinearLayout> <!-- 底部按钮 -->
<LinearLayout
android:id="@+id/customviewlayLink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/customviewlayMessage"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"> <Button
android:id="@+id/ortherbtnemil"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/linkbtnbged"
android:linksClickable="true"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:text="Email给作者" /> <Button
android:id="@+id/ortherbtnweb"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:linksClickable="true"
android:background="@drawable/linkbtnbged"
android:text="访问博客"
android:layout_marginLeft="10dp"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
核心java代码
/**
* 点击主界面按钮点监听
*
*/
private void onClickBtnListener() {
Builder builder=myBuilder(MainActivity.this);
/**调用Build的show方法设置为显示,并返回一个AlertDialog对象**/
final AlertDialog dialog=builder.show();
/**点击屏幕外侧对话框是否显示**/
dialog.setCanceledOnTouchOutside(false);
/**点击右侧的按钮关闭对话框**/
imageCloseDialog(dialog); sendEmailClickListener(dialog); openBlogListener(dialog);
} /**
* 访问blog的监听事件
* @param dialog
*/
private void openBlogListener(final AlertDialog dialog) {
Button ortherbtnweb = (Button)customView.findViewById(R.id.ortherbtnweb);
ortherbtnweb.setOnClickListener(new OnClickListener() { /* 点击访问博客按钮,访问我的博客网站
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Uri uri = Uri.parse("http://blog.csdn.net/asinzuo");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
dialog.dismiss();
}
});
} /**
* 发送邮件按钮事件
* @param dialog
*/
private void sendEmailClickListener(final AlertDialog dialog) {
Button ortherbtnemil = (Button) customView.findViewById(R.id.ortherbtnemil);
ortherbtnemil.setOnClickListener(new OnClickListener() { /* 启动发送邮件的服务
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.setType("message/rfc822") ; // 真机上使用这行
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"asinzuo@qq.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"windows风格dialog反馈");
i.putExtra(Intent.EXTRA_TEXT,"内容");
startActivity(Intent.createChooser(i, "选择应用"));
dialog.dismiss();
}
});
} /**
* 点击关闭图片,关闭弹出框
* @param dialog
*/
private void imageCloseDialog(final AlertDialog dialog) {
/**从customView中获得关闭按钮,返回值是一个ImageButton**/
ImageButton customviewtvimgCancel=(ImageButton)customView.findViewById(R.id.customviewtvimgCancel);
/**为ImageButton设置监听事件,使用匿名内部类实现**/
customviewtvimgCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/**调用AlertDiloag的dismiss方法,进行关闭**/
dialog.dismiss();
}
});
} /**
* 生成一个Builder对象
* @param mainActivity
* @return
*/
private Builder myBuilder(MainActivity mainActivity) {
// TODO Auto-generated method stub
/**
* 它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;
* 而findViewById()是找xml布局文件下的具体widget控件
*
*/ /**从指定的activity中获得一个LayoutInflater对象**/
final LayoutInflater inflater=this.getLayoutInflater();
/**inflate()的作用就是将一个用xml定义的布局文件查找出来,返回值是view类型**/
customView=inflater.inflate(R.layout.windowsdialog, null);
/**创建一个Builder对象**/
AlertDialog.Builder builder=new AlertDialog.Builder(mainActivity); return builder.setView(customView);
}
Android简单例子——AlertDialog的更多相关文章
- Android简单例子——IpHone样式AlertDialog
此例子源于网络,下载下来之后,自己加了写注释,作为总结,发到博客中,谢谢原作者 通过这个例子学到的东西 1.自定义对话框的使用 2.程序中使用颜色如何进行存放,增加复用性 3.加深线性布局.常用控件的 ...
- android json解析及简单例子+Android与服务器端数据交互+Android精彩案例【申明:来源于网络】
android json解析及简单例子+Android与服务器端数据交互+Android精彩案例[申明:来源于网络] android json解析及简单例子:http://www.open-open. ...
- Android简单逐帧动画Frame的实现(二)
Android简单逐帧动画Frame的实现 Android简单逐帧动画Frame的实现 1.逐帧动画 即是通过播放预先排序好的图片来实现动态的画面,感觉像是放电影. 2.实现步骤: 1. 在工程里 ...
- android中的AlertDialog具体概述
android的AlertDialog具体解释 AlertDialog的构造方法所有是Protected的.所以不能直接通过new一个AlertDialog来创建出一个AlertDialog. 要创建 ...
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
随机推荐
- RedHat/CentOS系统信息查看命令大全
系统# uname -a # 查看内核/操作系统/CPU信息# head -n 1 /etc/issue # 查看操作系统版本# cat /proc/cpuinfo ...
- bzoj 3226 [Sdoi2008]校门外的区间(线段树)
3226: [Sdoi2008]校门外的区间 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 615 Solved: 227[Submit][Stat ...
- poj 1696 Space Ant(模拟+叉积)
Space Ant Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3840 Accepted: 2397 Descrip ...
- 有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。
有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为 A = sum(a) - ...
- Ruby on Rails Session 1: How to Build a Ruby on Rails on the Ubuntu.
About Ruby on Rails Ruby on Rails is an application stack that provides developers with a framework ...
- phpeclipse xdebug 配置配置 -摘自网络
一.安装配置 1.访问 http://www.phpeclipse.com/ ,找到右边的 1.2.x dev nightly下的http://update.phpeclipse.com/update ...
- 关于ASSERT(断言)的作用
程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用.断言assert 是仅在Debug 版本起作用的宏,它用于检查“不应该”发生的情况 ...
- Android模拟器报"Failed To Allocate memory 8"错误的解决办法
此错误是我们在允许AVD时,选择了默认的AVD插件所致. 解决方法:减少分配的内存大小.修改AVD的config.ini配置文件,将选项“hw.ramSize=1024”从1024改为256.
- Ajax提交打开新窗口,浏览器拦截处理
//主要是添加同步处理 $.ajax({ url: "ashx/OrderHander.ashx?action=CheckRepeat", data: { "OrderI ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...