package com.loaderman.dialogdemo;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 提示对话框
*/
public void normal(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置标题
builder.setTitle("对话框标题");
//设置内容
builder.setMessage("我是对话框信息");
//显示确定按钮
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//点击确定按钮后调用
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已确定", Toast.LENGTH_SHORT).show();
}
});
//显示取消按钮
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//取消
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "已取消", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
/**
* 进度条对话框
*/
public void progress(View view) {
final ProgressDialog pd = new ProgressDialog(this);
//设置标题
pd.setTitle("进度条对话框");
//设置最大值
pd.setMax(100);
//设置进度条的样式
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new Thread() {
public void run() {
try {
for (int i = 1; i < 101; i++) {
sleep(100);
//设置进度条的进度
pd.setProgress(i);
}
//让进度条对话框消失
pd.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
//设置内容
// pd.setMessage("玩命下载中...");
pd.show();
}
  //列表单选对话框
public void single1(View view) {
final String[] items = {"java", ".net", "php"};
new AlertDialog.Builder(MainActivity.this).setTitle("选择语言")
.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
}).show();//显示对话框
}
  //列表带框的单选对话框
public void single2(View view) {
final String[] items = {"java", ".net", "php"};
new AlertDialog.Builder(MainActivity.this).setTitle("选择语言")
.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
dialog.cancel();
}
}).show();//显示对话框
}
  //多选复选框
public void more(View view) {
final String[] items = {"java", ".net", "php"};
new AlertDialog.Builder(MainActivity.this).setCancelable(false)
.setTitle("选择语言")
.setMultiChoiceItems(items, new boolean[]{false, true, false}, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
}
}
}) //选项复选框
.setPositiveButton("确认",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
}
}) //确认按钮
.show(); //显示对话框
}
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.loaderman.dialogdemo.MainActivity"> <Button
android:onClick="normal"
android:layout_width="match_parent"
android:text="提示对话框"
android:layout_height="wrap_content"/>
<Button
android:onClick="progress"
android:layout_width="match_parent"
android:text="进度对话框"
android:layout_height="wrap_content"/>
<Button
android:onClick="single1"
android:layout_width="match_parent"
android:text="列表对话框"
android:layout_height="wrap_content"/>
<Button
android:onClick="single2"
android:layout_width="match_parent"
android:text="单选带框的对话框"
android:layout_height="wrap_content"/>
<Button
android:onClick="more"
android:layout_width="match_parent"
android:text=" 多选对话框"
android:layout_height="wrap_content"/>
</LinearLayout>

效果图:


Context是Activity的父类
父类有的方法, 子类一定有,
子类有的方法,父类不一定有
当show一个Dialog时, 必须传Activity对象, 否则会出异常android.view.WindowManager$BadTokenException: Unable to add window-token null is
not for an application因为Dialog必须依赖Activity为载体才能展示出来,
所以必须将Activity对象传递进去,以后在使用Context的时候, 尽量传递Activity对象, 这样比较安全

Dialog对话框的几种方式使用实现的更多相关文章

  1. Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解

    Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解   在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...

  2. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  3. JS弹出对话框的三种方式

    JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...

  4. Android Dialog对话框的七种形式的使用

    参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...

  5. Swing中弹出对话框的几种方式(转)

    http://www.cnblogs.com/mailingfeng/archive/2011/12/28/2304289.html 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户 ...

  6. js弹出对话框的三种方式(转)

    原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...

  7. Android之UI--打造12种Dialog对话框

    最近有空,来把app中常用到的Dialog对话框写一篇博客,在app中很多地方都会用到Dialog对话框,今天小编我就给大家介绍Dialog对话框. 先看看效果图: 12种,可根据需求选择,上图可知, ...

  8. Easyui 创建dialog的两种方式,以及他们带来的问题

    $('#yy').dialog('open');//打开dialog 这地方要注意,加入你关闭窗口的地方使用$('#yy').dialog('destroy');那么你这个dialog就只能使用一次, ...

  9. 基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式

    在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交,方便页面和服务器后端进行数据的交互处理.本文主要介绍利用Jquery处理数据交互的几种方式,包括 ...

随机推荐

  1. current status of the installation and the internationalization of Samba 3.0

    Only about 8 months from release of Samba 3.0.0, there is beginning to be the transition from 2.2.x. ...

  2. 1、Bash Shell

    一.什么是Bash shell BashShell是一个命令解释器,它在操作系统的最外层,负责用户程序与内核进行交互操作的一种接口,将用户输入的命令翻译给操作系统,并将处理后的结果输出至屏幕. 当我们 ...

  3. 论文笔记:Integrated Object Detection and Tracking with Tracklet-Conditioned Detection

    概要 JiFeng老师CVPR2019的另一篇大作,真正地把检测和跟踪做到了一起,之前的一篇大作FGFA首次构建了一个非常干净的视频目标检测框架,但是没有实现帧间box的关联,也就是说没有实现跟踪.而 ...

  4. Beta冲刺——星期四

    这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <作业要求的链接> 团队名称 飞猪们 这个作业的目标 剩余任务预估,分配任务(开发,测试等).按要求提交当天冲刺报告. ...

  5. hadoop/hbase/hive单机扩增slave

    原来只有一台机器,hadoop,hbase,hive都安装在一台机器上,现在又申请到一台机器,领导说做成主备, 要重新配置吗?还是原来的不动,把新增的机器做成slave,原来的当作master?网上找 ...

  6. struts2使用注解的时候遇到的问题

    问题描述:     一切配置和注解都正确,但是无法访问action中的方法   解决问题:     之前将action层的包名命名成了web,改成action就对了   原因:     struts2 ...

  7. [转载]Spark-Task not serializable错误解析

    Spark-Task not serializable错误解析 2018年05月17日 15:33:03 沙拉控 阅读数:1509   在学习SparkStreaming的时候偶然出现的一个问题,先看 ...

  8. ogg中断处理

    ogg因为网络问题导致中断无法启动,需要重新抽取数据: --前滚抽取进程生成新的trail文件 alter extract ext147 etrollover alter ext147 extseqn ...

  9. shell小命令

    小括号的用途 cd ..; ls -l (cd ..; ls -l) 如果加了括号,则当前工作目录不发生改变 shell变量类型 环境变量 可以使用 echo  或者env 例如 env|grep 变 ...

  10. NServiceBus:使用自定义log4net

    安装扩展包:NServiceBus.Log4Net Endpoint.cs增加注入 log4net.Config.BasicConfigurator.Configure(); NServiceBus. ...