Dialog对话框的几种方式使用实现
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对话框的几种方式使用实现的更多相关文章
- Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解
Swing中弹出对话框的几种方式_JOptionPane.showMessageDialog等详解 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户的动作进行提示. Swing中 ...
- WPF中使用文件浏览对话框的几种方式
原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...
- JS弹出对话框的三种方式
JS弹出对话框的三种方式 我们用到了alert()方法.prompt()方法.prompt()方法,都是在网页有一个弹出框,那么就让我们探究一下他们之间的区别: 一.第一种:alert()方法 < ...
- Android Dialog对话框的七种形式的使用
参考资料:http://www.oschina.net/question/54100_32486 注:代码进行了整理 在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询 ...
- Swing中弹出对话框的几种方式(转)
http://www.cnblogs.com/mailingfeng/archive/2011/12/28/2304289.html 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户 ...
- js弹出对话框的三种方式(转)
原文地址:https://www.jb51.net/article/81376.htm javascript的三种对话框是通过调用window对象的三个方法alert(),confirm()和prom ...
- Android之UI--打造12种Dialog对话框
最近有空,来把app中常用到的Dialog对话框写一篇博客,在app中很多地方都会用到Dialog对话框,今天小编我就给大家介绍Dialog对话框. 先看看效果图: 12种,可根据需求选择,上图可知, ...
- Easyui 创建dialog的两种方式,以及他们带来的问题
$('#yy').dialog('open');//打开dialog 这地方要注意,加入你关闭窗口的地方使用$('#yy').dialog('destroy');那么你这个dialog就只能使用一次, ...
- 基于MVC4+EasyUI的Web开发框架经验总结(12)--利用Jquery处理数据交互的几种方式
在基于MVC4+EasyUI的Web开发框架里面,大量采用了Jquery的方法,对数据进行请求或者提交,方便页面和服务器后端进行数据的交互处理.本文主要介绍利用Jquery处理数据交互的几种方式,包括 ...
随机推荐
- DataGrip导出查询结果数据
1 按钮 2 选择保存位置即可
- 第六章· MySQL索引管理及执行计划
一.索引介绍 1.什么是索引 1)索引就好比一本书的目录,它能让你更快的找到自己想要的内容. 2)让获取的数据更有目的性,从而提高数据库检索数据的性能. 2.索引类型介绍 1)BTREE:B+树索引 ...
- pandas中Series对象下的str所拥有的方法(df["xx"].str)
在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...
- 1.Bacula各组件说明
1. Bacula各组件说明 Baula有三个服务,分别是bacula-sd用于管理storage.bacula-fd为bacula客户端.bacula-dir为bacula的核心组件机direct ...
- Easy Populate批量管理下载产品数据为空的解决办法
把原来的先删除:http://aaaaacom/admin/easypopulate.php?langer=remove
- Codeforces 1082 毛毛虫图构造&最大权闭合子图
A #include<bits/stdc++.h> using namespace std; typedef long long ll; , MAXM = ; //int to[MAXM ...
- 基于Hexo的个人博客搭建(下)
5.服务器端测试 —5.1 clone到/var/www/html git clone /home/git/repos/myblog.git /var/www/html chown -R git:g ...
- Vue-cli + express 构建的SPA Blog(采用前后端分离方案)
为什么学习并使用Vue 1.发展趋势 最近这几年的前端圈子,由于戏台一般精彩纷呈,从 MVC 到 MVVM,你刚唱罢我登场. backbone,AngularJS 已成昨日黄花,reactjs 如日中 ...
- MySQL_DBA整理
MySQL_DBA整理(转) 2011-01-26 16:51:41 分类: Mysql/postgreSQL 转自:丁丁:http://www.itpub.net/thread-831154-2- ...
- Java 代码的精优化
一.避免使用BigDecimal(double) BigDecimal(double) 存在精度损失风险,在精确计算或值比较的场景中可能会导致业务逻辑异常. 反例: // BigDecimal 反例 ...