Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框
1.AlertDialog类的功能特别强大,不仅可以生成带有按钮的提示对话框,还可以生成带列表的列表对话框,在使用AlertDialog类生成对话框时,只能生成带按钮的提示对话框,如果要生成带列表的提示对话框的话,需要使用AlertDialog.Builder类,接下来将介绍如何通过AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框。
2.首先,新建一个安卓项目,项目名为DialogTest,接下来布局所要显示的页面,打开res目录下的layout文件夹下的activity_main.xml文件,界面很简单,就一个按钮,点击此按钮弹出带取消,确定,中立的提示对话框,附上布局文件的代码:
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"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/showDialog"/></LinearLayout>
3.接下来开始写如何创建提示对话框的相应代码,不多说,直接附上MainActivity.java文件的代码,里面有简单的注释,其中如果要使用AlertDialog类来创建对话框时,就在按钮点击事件时调用openDialog1()方法,这里默认使用AlertDialog.Builder类来创建提示对话框,在点击事件里调用openDialog()方法:
MainActivity.java文件:
package com.dialogtest;import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button1=(Button)findViewById(R.id.button1);//获取按钮控件//点击按钮时,进行事件处理button1.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {// TODO Auto-generated method stubopenDialog();//使用AlertDialog.Builder类打开对话框的方法//openDialog1();//使用AlertDialog类打开对话框的方法}});}/*使用AlertDialog.Builder类创建一个带有取消,确定,中立的对话框*/protected void openDialog(){AlertDialog.Builder builder=new Builder(MainActivity.this);//使用AlertDialog.Builder类实例化一个对话框builder.setIcon(R.drawable.ic_launcher);//设置对话框的图标builder.setTitle("对话框...");//设置对话框的标题builder.setMessage("带取消、中立和确定按钮的对话框!!!");//设置要显示的内容//添加取消按钮builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}}); //添加确定按钮builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}});//添加中立按钮builder.setNeutralButton(R.string.neutral, new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}});builder.create().show();//创建对话框并显示}/*使用AlertDialog类创建一个带有取消,确定,中立的对话框*/public void openDialog1(){AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create();//使用AlertDialog类实例化一个对话框并创建对话框alert.setIcon(R.drawable.ic_launcher);//设置对话框的图标alert.setTitle("对话框...");//设置对话框的标题alert.setMessage("带取消、中立和确定按钮的对话框!!!");//设置要显示的内容//添加取消按钮alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}});//添加确定按钮alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了确定按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}});//添加中立按钮alert.setButton(DialogInterface.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stub}});alert.show();//显示对话框}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}}
4.使用AlertDialog类和AlertDialog.Builder类创建提示对话框的效果是一样的,但是创建提示对话框的代码有区别,区别在于实例化的类不同。
(1).AlertDialog类里创建对话框时:
AlertDialog alert=new AlertDialog.Builder(MainActivity.this).create();//使用AlertDialog类实例化一个对话框并创建对话框
生成按钮时是用setButton方法,其中添加取消按钮时代码如下:
//添加取消按钮alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}});
后面必须把这个提示对话框显示在界面当中,使用下面的代码:
alert.show();//显示对话框
(2).AlertDialog.Builder类里创建对话框时,只是实例化了个类,并没有创建出来:
AlertDialog.Builder builder=new Builder(MainActivity.this);//使用AlertDialog.Builder类实例化一个对话框
生成按钮时是用setNegativeButton(),setNeutralButton(),setPositiveButton()方法分别为对话框添加取消按钮,中立按钮和确定按钮,其中添加取消按钮时代码如下:
//添加取消按钮builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {// TODO Auto-generated method stubToast.makeText(MainActivity.this, "你点击了取消按钮", Toast.LENGTH_SHORT).show();//弹出Toast显示消息提示框}});
后面必须把这个提示对话框创建并显示出来,使用下面这一行代码:
builder.create().show();//创建对话框并显示
5.接下来附上这里所需要的字符串资源,附上res目录下values文件夹的strings.xml文件:
strings.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">DialogTest</string><string name="hello_world">Hello world!</string><string name="action_settings">Settings</string><string name="showDialog">点击按钮显示带取消,确定,中立的对话框</string><string name="neutral">中立</string>
</resources>
6.接下来运行此安卓项目,运行效果如下:
点击此按钮,出现效果如下:
其中有我在MainActivity.java文件设置提示对话框的图片,标题,显示的信息和为此对话框添加的三个按钮,点击提示对话框里的取消按钮,效果如下:
点击对话框里的中立按钮,在为对话框添加按钮时没做任何事件处理,所以点击之后效果如下:
点击提示对话框里的确定按钮,效果如下:
7,以上就是这篇博客的内容,仅供大家学习参考,写得不好,请见谅,如果有什么错误请指出,谢谢!
Android之使用AlertDialog类和AlertDialog.Builder类创建带取消,确定,中立的对话框相关推荐
- java中System类、String Builder类简介
System类: System类为系统类,此包位于java.lang.System下,可以获得与系统相关的信息或系统操作,如: public class SystemClass{public stat ...
- Android之使用AlertDialog.Builder类创建带列表的对话框和带自己所布局视图的对话框
1.首先,新建一个安卓项目,项目名称为DialogTest1,首先,进行界面布局,打开布局文件res下的layout文件夹下的activity_main.xml文件,附上activity_main的代 ...
- android弹出确认对话点击取消,Android 点击AlertDialog上的确定和取消按钮,使对话框不消失...
Android中的AlertDialog弹出框在被点击时, 无论点击哪个按钮都会关闭窗口. 但是有时候我们不需要它关闭,例如输入用户名和密码,输错了,提示重新输入. 那么怎么做到点击确定或者取消按钮不 ...
- 【Android 常见控件使用】AlertDialog(对话框)详解
文章目录 AlertDialog(对话框)详解 本节引言 1.基本使用流程 2.几种常用的对话框使用示例 3.通过Builder的setView()定制显示的AlertDialog AlertDial ...
- 【Android 组件化】路由组件 ( 生成 Root 类记录模块中的路由表 )
文章目录 一.Root 表作用 二.生成 Root 表 三.完整注解处理器代码 及 生成的 Java 代码 ( 仅供参考 ) 1.注解处理器代码 2.app 模块中的注解类生成的 Java 源码 3. ...
- Android 常用设计模式——观察者模式,单例模式,Builder模式,原型模式,命令模式
设计模式(Design pattern)是一套被反复使用的代码设计经验的总结.使用设计模式的目的是为了可重用代码让代码更容易被他人理解 设计模式的运用,有助于代码的维护与拓展.任何模式的出现,都是为了 ...
- 【转】 Android快速开发系列 10个常用工具类 -- 不错
原文网址:http://blog.csdn.net/lmj623565791/article/details/38965311 转载请标明出处:http://blog.csdn.net/lmj6235 ...
- Android快速开发系列 10个常用工具类
目录(?)[+] 转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38965311,本文出自[张鸿洋的博客] 打开大家手上的项目,基 ...
- android文件读取工具类,Android 下读取Assets Properties操作封装工具类
Android 下读取Assets Properties操作封装工具类 发布时间:2018-06-03作者:laosun阅读(2081) 为了方便使用,首先创建BaseApplication类,如下所 ...
最新文章
- python操作文件open_python:open/文件操作
- JAVA 对象序列化(二)——Externalizable
- 我的第一个JW项目——网上商城设计说明书(五)
- gtw-050090|执行拦截器时发生异常_执行流程 | 你真的了解Spring AOP的执行顺序吗?...
- web开发者工具,你必须知道的CSS盒模型,架构师必备!
- android 蓝牙sco stream_voice_call,android TTS输出总是要 A2DP_android_开发99编程知识库
- gist.github.com 被墙无法访问解决办法
- python文件下载学习
- 小艾机器人apk_小艾QQ群机器人安卓版
- 内网通免广告/积分码
- 平稳时间序列以及MATLAB相关工具箱学习笔记
- C++11 function类模板
- 《辛德勒的名单》观后感
- 再谈微软复兴,纳德拉与库克、马斯克、皮查伊在管理上有什么不同
- 1.STM32F103-点亮第一颗LED
- 四阶段:第24周 微服务下Sentinel流量防控卫兵 apollo相关配置 一般有用
- 9年前,字节跳动第一个安卓工程师是这样工作的
- 南卫理公会大学计算机科学,南卫理公会大学计算机科学研究生语言及申请要求-费用-课程设置...
- Windows支持WGET命令
- android 页面默认不弹软键盘_Android 软键盘的全面解析,让你不再怕控件被遮盖!...
热门文章
- easyUI combobox启用禁用功能写法
- SCU 4445 Right turn(dfs)题解
- 用VSCode开发一个asp.net core2.0+angular5项目(5): Angular5+asp.net core 2.0 web api文件上传...
- A - Super Jumping! Jumping! Jumping!
- AngularJS日期格式化
- mysql示例数据库
- 空间直角坐标转换之仿射变换(转)
- Decorator(装饰器)
- Modularity(模块化-ES6)
- ubuntu无法打开Firefox