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. 身份证js正则

    /* 根据[中华人民共和国国家标准 GB 11643-1999]中有关公民身份号码的规定,公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成.排列顺序从左至右依次为:六位数字地址码,八位 ...

  2. Hadoop_04_Hadoop 的HDFS客户端shell命令

    1.Hdfs shell客户端命令操作: 1.1.查看命令列表:hadoop fs 帮助如下: Usage: hadoop fs [generic options] [-appendToFile &l ...

  3. win10电脑配置

    微信 QQ 电脑管家 Chrome 坚果云 Sublime VLC 网易云音乐 Acrobat Reader DC PS git potplayer TeamViewer 有道云笔记/协作 百度网盘/ ...

  4. Perl环境安装

    在我们开始学习 Perl 语言前,我们需要先安装 Perl 的执行环境. Perl 可以在以下平台下运行: Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, Sun ...

  5. LeetCode3.无重复字符的最大子串

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...

  6. Tomcat 调优测试

    测试环境: OS: Ubuntu14.04 64位 (运行在Docker1.9) CPU: Intel i3 双核四线程 Mem: 8G Tomcat版本: Tomcat8.5 Java SDK版本: ...

  7. 基于Kibana的可视化监控报警插件sentinl入门

    sentinl是什么 Kibi/Kibana Alert & Reporting App Watching your data, 24/7/365 sentinl是一个免费的kibana预警与 ...

  8. IOS下图片不能显示问题的解决办法

    最近遇到这样一个问题,在HTML5手机页面中,直接给<img>标签设置宽高,即便图片路径正常,在IOS真机下也是无法显示的,而在安卓以及浏览器的模拟真机上都是正常显示的,这是为什么呢? h ...

  9. [Spring Boot] Aspect Intro

    Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another ...

  10. Linux命令(Ubuntu)

    VMware 打开命令模式快捷键:  Ctrl + Alt + T VMware 里面Ubuntu不能全屏, 本来想着习惯就好, 知道今天看一些信息不能全部显示, 最终不能忍了. 上网找了很多方法 主 ...