AlertDialog基本用法详解
AlertDialog简单介绍:
AlertDialog可以在当前活动界面弹出一个对话框,用于提示一些重要信息或是警告内容。
AlertDialog置于所有页面元素之上,能够屏蔽其他控件的交互。
由于AlertDialog的构造方法被声明为protected,所以我们不能使用new来创建AlertDialog对象。
Android为我们提供另外一个类AlertDialog.Builder,用它可以创建AlertDialog对象实例,用show()方法显示。
AlertDialog有几个基本方法,
例如:setTitile() setMessage() setCancelable() setPositiveButton() setNegativeButton() setNeutralButton() 等等。
这些方法通过看字面意思,就差不多明白它的作用。
下面就给读者介绍AlertDialog的基本使用方法。
首先是布局文件,在布局文件中添加三个按钮分别来处理相应的三个不同的AlertDialog。
关键代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/btn_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="@string/confirm"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/cancel"/>
<Button
android:id="@+id/btn_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search"/> </LinearLayout>
效果图如下:

下面就开始写java代码
为了便于统一管理各个活动类,创建一个基本活动类BaseActivity.java
关键代码如下:
public class BaseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置无标题 节省屏幕空间
requestWindowFeature(Window.FEATURE_NO_TITLE);
//打印正在运行活动的名称 调试所用
Log.d("BaseActivity", getClass().getSimpleName());
}
}
基本工作准备完毕,开始重头戏:MainActivity.java extends BaseActivity
读者也可以直接继承Activity
第一步是必要控件 视图创建 控件加载以及设置监听
public class MainActivity extends BaseActivity {
//创建三个按钮
private Button btn_confirm;
private Button btn_cancel;
private Button btn_search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//加载按钮视图并添加监听事件
btn_confirm = (Button) findViewById(R.id.btn_confirm);
btn_cancel = (Button) findViewById(R.id.btn_cancel);
btn_search = (Button) findViewById(R.id.btn_search);
btn_confirm.setOnClickListener(new Listener());
btn_cancel.setOnClickListener(new Listener());
btn_search.setOnClickListener(new Listener());
}
第二步自定义监听类Listener 管理三个按钮 并在每个按钮的点击事件中创建AlertDialog
首先是带一个按钮的AlertDialog,只需添加一个setPositiveButton()
按钮点击直接Toast
代码如下:
private class Listener implements OnClickListener {
AlertDialog.Builder dialog = null;
@Override
public void onClick(View view) {
// 利用switch对三个按钮分别进行监听
switch (view.getId()) {
case R.id.btn_confirm:
dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("My AlertDialog");
dialog.setMessage("The Google Play service has stoped");
dialog.setCancelable(false);
dialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You clicked OK", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
break;
效果图如下:

其次是带有两个按钮的AlertDialog 添加setPositiveButton()和setNegativeButton()
按钮点击依旧采用Toast处理
代码如下:
case R.id.btn_cancel:
dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("My AlertDialog");
dialog.setMessage("Are you sure you want to delete it");
dialog.setCancelable(false);
dialog.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You clicked Yes", Toast.LENGTH_SHORT).show();
}
});
dialog.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You clicked No",Toast.LENGTH_SHORT).show();
}
});
dialog.show();
break;
效果图如下:

最后是带有三个按钮的AlertDialog 逻辑和前者相似,只需增加一个setNeutralButton()即可
按钮点击调用自定义方法gotoWeb()
case R.id.btn_search:
dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("My AlertDialog");
dialog.setMessage("Select the search engines");
dialog.setCancelable(false);
dialog.setPositiveButton("Google", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String URL = "http://www.google.com";
gotoWeb(URL);
}
});
dialog.setNegativeButton("Baidu", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String URL = "http://www.baidu.com";
gotoWeb(URL);
}
});
dialog.setNeutralButton("Bing", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String URL = "http://cn.bing.com";
gotoWeb(URL);
}
});
dialog.show();
break;
default:
break;
}
}
}
效果图如下:

在第三个AlertDialog中调用了一个自定义方法gotoWeb(),使用意图调转到指定网站的访问
具体代码如下:
private void gotoWeb(String URL) {
Uri uri = Uri.parse(URL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
finish();
}
注意:MainActivity被拆分成多个部分分块解释。
大功告成,本文介绍AlertDialog最基本的用法,当然还有很多炫酷的自定义效果,有兴趣可以自行深入学习。
转载请注明出处:http://www.cnblogs.com/michaelwong/p/4114944.html
AlertDialog基本用法详解的更多相关文章
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
- mysql中event的用法详解
一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...
- CSS中伪类及伪元素用法详解
CSS中伪类及伪元素用法详解 伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...
- c++中vector的用法详解
c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...
- AngularJS select中ngOptions用法详解
AngularJS select中ngOptions用法详解 一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...
- systemctl命令用法详解
systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...
- CSS3的@keyframes用法详解:
CSS3的@keyframes用法详解:此属性与animation属性是密切相关的,关于animation属性可以参阅CSS3的animation属性用法详解一章节. 一.基本知识:keyframes ...
随机推荐
- sqlyog绿色破解版
http://pan.baidu.com/s/1mghyUrY 下载地址
- 【Android】Fragment如何获取子Fragment
今天搞了个嵌套的Fragment,通过外部的Fragment获取的子Fragment代码: this.navigationBar = (HXKJCargoNavigationView) getFrag ...
- C#中List和数组之间的转换
一.List转数组 (从List<string>转到string[]) C# 代码 复制 List<string> listS=new List<string&g ...
- 深搜基础题目 杭电 HDU 1241
HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...
- mysql导出命令
数据库备份 /data/mysql/bin/mysqldump -hlocalhost -u'root' -p'do' my_db --single-transaction -q | gzip > ...
- gridView行号的显示
我们在进行开发的时候,很多地方希望dataGridview或girdView显示行号,这里我来说一下两种的实现方法 在girdView中很简单很好实现,我在这里写一下代码,具体其他功能可以看其带的DE ...
- jquery EsayUi 里一个小弹框
网站后台大多的数据展示就都用和此插件有着密切的关系: 来用一下这个小弹框吧: 一个Html里面的代码 <link rel='stylesheet' type='text/css' href='c ...
- mvc 跳转到另一个页面 Controller带参数
跳转到链接 // Controller public ActionResult Detail(int MessageId) { BLL.ZQS ...
- SQL Server 移动master 数据库
第一步: 告诉SQL Server 下次启动时master数据库的文件在哪里!我想们一定想到了(这样做是不对的,它对master不起作用,第二步开始正确的做法) alter database mast ...
- python学习day2(一)
一.上周作业回顾 1.登陆接口: 思路流程: 1.登陆,三次锁定用户 2.用户信息文件,黑名单文件 3.检测黑名单,如输入账号在黑名单中存在,不允许登陆 4.用户密码判断 主要知识点:while,fo ...