有普通对话框,单选对话框,复选对话框,进度条的两种实现方法话不多说,直接上代码

activity_main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="普通对话框"
android:onClick="normalDialog"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="单选对话框"
android:onClick="singleDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复选对话框"
android:onClick="MultiDialog"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:onClick="progressDialog"/>
<Button
android:id="@+id/downlodebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="进度条对话框"
android:onClick="progressDialog"/>
</LinearLayout> </android.support.constraint.ConstraintLayout>

MainActivity.java:

 package com.lgqrlchinese.dialogtext;

 import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.SystemClock;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
protected String result; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progerssNow();
} //普通对话框
public void normalDialog(View view) {
//构建器
AlertDialog.Builder b = new AlertDialog.Builder(this);
//设置标题
b.setTitle("普通对话框");
//设置提示信息
b.setMessage("是否确定退出");
//设置图标
b.setIcon(R.drawable.ic_launcher_background);
//添加确定按钮
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
//添加取消按钮
b.setNegativeButton("取消", null);
//创建对话框
b.create();
//显示
b.show();
} //单选对话框
public void singleDialog(View view) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("单选对话框");
//选项内容
final String item[] = {"A", "B", "C", "D"};
//setSingleChoiceItems(显示的选项内容,默认值,监听事件)
b.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//取出数组中的数据
result = item[i];
//吐司显示出来
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
});
//设置确定按钮
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "选择的结果是" + result, Toast.LENGTH_SHORT).show();
}
}); b.create();
b.show();
} //复选对话框
public void MultiDialog(View view) {
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("复选框对话框");
b.setIcon(R.drawable.ic_launcher_background);
//设置选项
final String item[] = {"A", "B", "C", "D"};
//设置默认值
final boolean chechedItem[] = {true, false, true, true};
//设置多选setMultiChoiceItems(选项数组,默认值,监听事件)
b.setMultiChoiceItems(item, chechedItem, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) { }
});
b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StringBuffer stringBuffer = new StringBuffer();
for (int i1 = 0; i1 < chechedItem.length; i1++) {
if (chechedItem[i1]) {
stringBuffer.append(item[i1] + " ");
}
}
Toast.makeText(getApplicationContext(), stringBuffer.toString(), Toast.LENGTH_SHORT).show();
}
});
b.create();
b.show();
} //进度条对话框
/*
* 在API level 26 中,ProgressDialog被声明不赞成使用,应使用的替代方法是ProgressBar
* */
public void progressDialog(View view) {
final ProgressDialog progressDialog = new ProgressDialog(this);
//设置标题
progressDialog.setTitle("正在加载中》》》");
//设置样式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//(模拟下载)线程睡眠
new Thread() {
@Override
public void run() {
//设置最大值
progressDialog.setMax(100);
//设置当前值
for (int i = 0; i < 100; i++) {
progressDialog.setProgress(i);
SystemClock.sleep(500);
}
progressDialog.dismiss();
}
}.start();
progressDialog.show();
} //进度条对话框(推荐使用)
public void progerssNow() {
LinearLayout mainLayout;
Button downlodebutton;
mainLayout = findViewById(R.id.mainLinearLayout);
downlodebutton = findViewById(R.id.downlodebutton);
final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
downlodebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Thread() {
@Override
public void run() { progressBar.setMax(100);
for (int i = 0; i < 100; i++) {
progressBar.setProgress(i);
SystemClock.sleep(12);
} }
}.start();
}
});
mainLayout.addView(progressBar);
}
}

四种对话框(dialog)的简单使用方法的更多相关文章

  1. [转]分享php中四种webservice实现的简单架构方法及实例

    FROM : http://www.itokit.com/2012/0417/73615_2.html 本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的P ...

  2. 分享php中四种webservice实现的简单架构方法及实例

    一:PHP本身的SOAP所有的webservice都包括服务端(server)和客户端(client).要使用php本身的soap首先要把该拓展安装好并且启用.下面看具体的code首先这是服务端实现: ...

  3. 分享php中四种webservice实现的简单架构方法及实例(转)

    本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC,以及使用二进制传输数据流的 HessianPHP,那么一下就简单的介绍下这几种webserv ...

  4. 分享php中四种webservice实现的简单架构方法及实例[转载]

    [转载]http://www.itokit.com/2012/0417/73615.html 本人所了解的webservice有以下几种:PHP本身的SOAP,开源的NUSOAP,商业版的PHPRPC ...

  5. Map集合遍历的四种方式理解和简单使用-----不能for循环遍历

    Map集合遍历的四种方式理解和简单使用   ~Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值 1:无非就是通过map.keySet()获取到值,然后 ...

  6. Android下常见的四种对话框

    摘要:在实际开发过程有时为了能够和用户进行很好的交互,需要使用到对话框,在Android中常用的对话框有四种:普通对话框.单选对话框.多选对话框.进度对话框. 一.普度对话框 public void ...

  7. 【Android】Android 8种对话框(Dialog)

    1.写在前面 Android提供了丰富的Dialog函数,本文介绍最常用的8种对话框的使用方法,包括普通(包含提示消息和按钮).列表.单选.多选.等待.进度条.编辑.自定义等多种形式,将在第2部分介绍 ...

  8. ThinkPHP中连接mysql数据库的四种实用和通用的连接方法

    ThinkPHP内置了抽象数据库访问层,把不同的数据库操作封装起来,我们只需要使用公共的Db类进行操作,而无需针对不同的数据库写不同的代码和底层实现,Db类会自动调用相应的数据库适配器来处理.目前的数 ...

  9. Map集合遍历的四种方式理解和简单使用

    ~Map集合是键值对形式存储值的,所以遍历Map集合无非就是获取键和值,根据实际需求,进行获取键和值 1:无非就是通过map.keySet()获取到值,然后根据键获取到值 for(String s:m ...

  10. Android 关于Activity的四种启动模式的简单介绍

    Activity启动模式设置: <activity android:name=".MainActivity" android:launchMode="standar ...

随机推荐

  1. vue 生产环境 background 背景图不显示原因

    通常我们使用img标签引入文件,npm run build 打包后 ,因为img为html标签,打包后他的路径是由index.html开始访问的,他走static/img/'图片名'是能正确访问到图片 ...

  2. python之psutil模块详解(Linux)--小白博客

    Python-psutil模块 windows系统监控实例,查询 https://www.cnblogs.com/zhou2019/p/10567282.html 1.简单介绍 psutil是一个跨平 ...

  3. BeautifulSoup库

    '''灵活又方便的网页解析库,处理高效,支持多种解析器.利用它不用编写正则表达式即可方便的实现网页信息的提取.''' BeautifulSoup库包含的一些解析库: 解析库 使用方法 优势 劣势 py ...

  4. web网站css,js更新后客户浏览器缓存问题,需要刷新才能正常展示的解决办法

    问题描述 最近将公司官网样式进行了调整,部署到服务器后访问发现页面展示不正常,但是刷新之后就会展示正常. 问题分析 研究之后发现可能的原因有 css文件过大,加载缓慢 本地缓存问题,虽然服务器修改了c ...

  5. 架构 规则引擎 quartz

    通向架构师的道路(第五天)之tomcat集群-群猫乱舞-云栖社区-阿里云https://yq.aliyun.com/articles/259343 商业规则引擎和开源规则引擎的测试对比 - qq_39 ...

  6. async并发处理

  7. Spring boot 将配置文件属性注入到一个bean中

    现在要做的就是将如下配置文件中的内容注入到一个bean 名为Properties中. Redis.properties配置文件中的内容如下: Properties java bean中代码如下,注意注 ...

  8. MT4用EA测试历史数据时日志出现:stopped because of stop out

    今天用嘉盛的MT4测试一个EA,谁知道才走了十几天数据就完 了,看结果本金也没亏完啊,才亏了一半,而且我测的是1年的时间. 查看日志一有条警告:stopped because of stop out, ...

  9. python(Django之组合搜索、JSONP、XSS过滤 )

    一.组合搜索 二.jsonp 三.xss过滤 一.组合搜索 首先,我们在做一个门户网站的时候,前端肯定是要进行搜索的,但是如果搜索的类型比较多的话,怎么做才能一目了然的,这样就引出了组合搜索的这个案例 ...

  10. python学习笔记(11)--文件与数据格式化

    文件的概念: 文件是数据的抽象和集合,是存储在辅助存储器上的数据序列,文件是数据存储的一种形式,文件的展现形态,文本文件和二进制文件. 文本文件输出: f.txt文件保存:“我是中国人” >&g ...