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

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. flink1.7自定义source实现

    flink读取source data 数据的来源是flink程序从中读取输入的地方.我们可以使用StreamExecutionEnvironment.addSource(sourceFunction) ...

  2. ASP.Net Core 运行错误 Http Error 502.5 解决办法

    Http Error 502.5 - Process Failure 如果你看到上面这张图片了的话,说明你在本地运行的时候报错了. 尤其好多都是我的群友,说下情况. 这个一般是本地的.NET Core ...

  3. Django Rest framework基础使用之Request/Response

    1.Request restframework提供了一个Request对象(rest_framework.request.Request) Request对象继承了Django默认的HttpReque ...

  4. Random()种子数

    Random rand =new Random(25); int i; i=rand.nextInt(100); 初始化时25并没有起直接作用,rand.nextInt(100);中的100是随机数的 ...

  5. ZOJ - 1610 经典线段树染色问题

    这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...

  6. c语言之sizeof的细节

    关于sizeof,我们知道sizeof并不是一个函数,尽管通常我们会用sizeof()用法(这是c语言的坑),在此关于sizeof的一些关键不被认知的进行一下总结: # include "i ...

  7. Linux安装Apache常见报错(二)

    配置Apache提示报错configure error: APR could not be located. Please use the --with-apr option. 解决办法: ./con ...

  8. [已解决]关于python无法显示中文的问题:SyntaxError: Non-ASCII character '\xe4' in file test.py on line 3, but no encoding declared。

    想在python代码中输出汉字.但是老是出现SyntaxError: Non-ASCII character '\xe4' in file test.py on line , but no encod ...

  9. Django之Django终端打印SQL语句

    Django之Django终端打印SQL语句 在Django项目中,settings.py文件中,在最后添加如下代码即可实现在Django终端打印SQL语句. LOGGING = { 'version ...

  10. 移动端和PC端页面常用的弹出层

    我们在页面的时候,很多时候用到了弹出层,消息提醒,确认框等等,统一样式的弹出框可以使页面更加优美.在此,我整理一下我们项目的移动端和PC端页面常用的弹出层. 一.移动端 我们需在页面引入弹出框的样式和 ...