Android开发中ProgressDialog简单用法示例
作者:guochongcan 发布时间:2021-11-03 10:17:37
标签:Android,ProgressDialog
本文实例讲述了Android开发中ProgressDialog简单用法。分享给大家供大家参考,具体如下:
网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下:
建立android工程等工作都略去,Google一下就可以了。
下面来介绍主要的Activity
ProgressBarDemo.java
package com.lveyo.android.demo.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ProgressBarDemo extends Activity {
private TextView statusTextView;
private Button beginBtn;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusTextView = (TextView)findViewById(R.id.status);
beginBtn = (Button)findViewById(R.id.beginBtn);
setListener();
}
/**
* 用Handler来更新UI
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//关闭ProgressDialog
progressDialog.dismiss();
//更新UI
statusTextView.setText("Completed!");
}};
/**
* 点击按钮事件listener
*/
private void setListener(){
beginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示ProgressDialog
progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
//新建线程
new Thread(){
@Override
public void run() {
//需要花时间计算的方法
Calculation.calculate(4);
//向handler发消息
handler.sendEmptyMessage(0);
}}.start();
}
});
}
}
package com.lveyo.android.demo.progressbar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ProgressBarDemo extends Activity {
private TextView statusTextView;
private Button beginBtn;
private ProgressDialog progressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
statusTextView = (TextView)findViewById(R.id.status);
beginBtn = (Button)findViewById(R.id.beginBtn);
setListener();
}
/**
* 用Handler来更新UI
*/
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
//关闭ProgressDialog
progressDialog.dismiss();
//更新UI
statusTextView.setText("Completed!");
}};
/**
* 点击按钮事件listener
*/
private void setListener(){
beginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示ProgressDialog
progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
//新建线程
new Thread(){
@Override
public void run() {
//需要花时间计算的方法
Calculation.calculate(4);
//向handler发消息
handler.sendEmptyMessage(0);
}}.start();
}
});
}
}
Calculation.java
package com.lveyo.android.demo.progressbar;
/**
* 示意方法
* @author lveyo
*
*/
public class Calculation {
public static void calculate(int sleepSeconds){
try {
Thread.sleep(sleepSeconds * 1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
package com.lveyo.android.demo.progressbar;
/**
* 示意方法
* @author lveyo
*
*/
public class Calculation {
public static void calculate(int sleepSeconds){
try {
Thread.sleep(sleepSeconds * 1000);
} catch (Exception e) {
// TODO: handle exception
}
}
}
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/beginBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/beginBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
在android中,通常我们无法在单独的线程中更新UI,而要在主线程中,这也就是为什么我们要使用 Handler了,当handler收到消息中,它会把它放入到队列中等待执行,通常来说这会很快被执行。
希望本文所述对大家Android程序设计有所帮助。
来源:http://guocc.iteye.com/blog/1181467
0
投稿
猜你喜欢
- 概述在以下示例中,将介绍在PDF文档页面设置页面切换按钮的方法。示例中将页面切换按钮的添加分为了两种情况,一种是设置按钮跳转到首页、下页、上
- 概述Kryo 是一个快速序列化/反序列化工具,依赖于字节码生成机制(底层使用了 ASM 库),因此在序列化速度上有一定的优势,但正因如此,其
- 前言我们在开发Web应用时,肯定要为用户提供上传的功能,比如用户上传一张图像作为头像等。为了能上传文件,我们必须将表单的method设置为P
- 这篇文章主要介绍了spring如何实现两个xml配置文件间的互调,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- json数据交互1.为什么要进行json数据交互json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。比
- Exception e中e的getMessage()和toString()方法的区别:示例代码1:public class TestInfo
- 配置文件请看上篇Java实现redis https://www.jb51.net/article/190922.htm下面测试redis的集
- 基本步骤三数取中在快排的过程中,每一次我们要取一个元素作为枢纽值,以这个数字来将序列划分为两部分。在此我们采用三数取中法,也就是取左端、中间
- 前提在Windows下进行数据处理的时候最常见的情况莫过于读取Microsoft的Excel文件了,Excel的普及率惊人,是事实上的标准。
- 在做相机开发时,遇到一个问题,就是需要监听屏幕旋转。最简单的就是使用onConfigurationChanged()和Orientation
- 本文实例讲解了iOS从背景图中取色的代码,分享给大家供大家参考,具体内容如下实现代码:void *bitmapData; //内存空间的指针
- 1. 日志框架的选择:(这两个框架,springBoot已经整合,无需引入jar包)2. 在resources目录下配置logback-sp
- seata-1.4.0安装及使用 1、简介Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。
- Java 实现FTP服务实例详解1、FTP简介 FTP
- spring 多文件配置:1、properties文件2、YAML文件一、properties文件在 Spring Boot 中, 多环境配
- 一、二叉排序树定义1.二叉排序树的定义二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tr
- 前言MyBatis中也提供了注解式开发⽅式,采⽤注解可以减少Sql映射⽂件的配置。 当然,使⽤注解式开发的话,sql语句是写在java程序中
- 本文实例讲述了java实现的n*n矩阵求值及求逆矩阵算法。分享给大家供大家参考,具体如下:先来看看运行结果:java版的写出来了,用的跟c语
- 一、错误处理原理分析使用SpringBoot创建的web项目中,当我们请求的页面不存在(http状态码为404),或者器发生异常(http状
- 在需要线程同步的时候如何选择合适的线程锁?例:选择可以存入到常量池当中的对象,String对象等public class SyncTest{