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基本用法详解的更多相关文章

  1. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

  2. @RequestMapping 用法详解之地址映射

    @RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...

  3. linux管道命令grep命令参数及用法详解---附使用案例|grep

    功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...

  4. mysql中event的用法详解

    一.基本概念mysql5.1版本开始引进event概念.event既“时间触发器”,与triggers的事件触发不同,event类似与linux crontab计划任务,用于时间触发.通过单独或调用存 ...

  5. CSS中伪类及伪元素用法详解

    CSS中伪类及伪元素用法详解   伪类的分类及作用: 注:该表引自W3School教程 伪元素的分类及作用: 接下来让博主通过一些生动的实例(之前的作业或小作品)来说明几种常用伪类的用法和效果,其他的 ...

  6. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  7. AngularJS select中ngOptions用法详解

    AngularJS select中ngOptions用法详解   一.用法 ngOption针对不同类型的数据源有不同的用法,主要体现在数组和对象上. 数组: label for value in a ...

  8. systemctl命令用法详解

    systemctl命令用法详解系统环境:Fedora 16binpath:/bin/systemctlpackage:systemd-units systemctl enable httpd.serv ...

  9. CSS3的@keyframes用法详解:

    CSS3的@keyframes用法详解:此属性与animation属性是密切相关的,关于animation属性可以参阅CSS3的animation属性用法详解一章节. 一.基本知识:keyframes ...

随机推荐

  1. java工厂类与反射机制

    java 简单工厂类 2012-04-22 15:44:07|  分类: java |  标签:java工厂类  简单工厂类  |举报|字号 订阅     简单工厂模式需要由以下角色组成: 接口    ...

  2. iOS开发--系统通讯录的访问与添加联系人

    公司项目有访问通讯录的需求,所以开始了探索之路.从开始的一无所知,到知识的渐渐清晰.这一切要感谢广大无私分享的 “coder”,注:我是尊称的语气! 苹果提供了访问系统通讯录的框架,以便开发者对系统通 ...

  3. zoj 1078

    非常简单的题,没什么好说的.唯一值得一提的就是在判断是否是回文的时候只需遍历一半的元素即可,稍微提高一点性能. #include<iostream> using namespace std ...

  4. PSR-2 Coding Style Guide

    本文主要是对PSR-2 的简单翻译. 英文源址 http://www.php-fig.org/psr/psr-2/ PSR2继承和扩展PSR1--基本编码规范 本手册的目的是使用一系列共同遵守的编码格 ...

  5. redhat系列yum本地源配置

    1.挂载光盘,本示例挂载在/mnt下. 2.清除系统带的.repo文件,rm -f /etc/yum.repos.d/* 3.编辑自己的repo文件,内容如下: [local_server]   (库 ...

  6. Noldbach problem

    Description Noldbach problem time limit per test: 2 seconds memory limit per test: 64 megabytes inpu ...

  7. In Depth : Android Shutdown Sequence

    What happened when I long press power button ?What is shutdown sequence ?How is it different from de ...

  8. js返回当前时间的毫秒数

    Date.now(); +new Date(); new Date().getTime();

  9. Entity Framework学习笔记

    原文地址:http://www.cnblogs.com/frankofgdc/p/3600090.html Entity Framework学习笔记——错误汇总   之前的小项目做完了,到了总结经验和 ...

  10. 常见 wifi热点的linux 驱动

    小度Wifi.360Wifi Windows.linux驱动 小度wifi什么的就是一个无线网卡,当然可以自由使用,然官方却说不支持无限网卡功能… 现提供Windows平台和linux平台的驱动安装方 ...