Android开发中ProgressDialog简单用法示例
作者:guochongcan 时间:2021-11-03 10:17:37
本文实例讲述了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
标签:Android,ProgressDialog
0
投稿
猜你喜欢
详解Spring Boot Profiles 配置和使用
2021-10-05 22:54:57
Mapper批量插入Oracle数据@InsertProvider注解
2023-02-11 15:13:40
C#多线程学习之(三)生产者和消费者用法分析
2021-08-11 01:55:26
使用Java 实现一个“你画手机猜”的小游戏
2021-06-03 19:19:27
C#文件分割的方法
2023-09-17 22:20:56
Java面向对象基础知识之抽象类和接口
2023-02-10 03:02:58
Spring Boot+Drools规则引擎整合详解
2023-11-28 20:33:00
Java实战之酒店人事管理系统的实现
2021-06-02 16:44:37
C#多线程之线程同步WaitHandle
2022-08-10 10:16:12
SpringBoot AOP AspectJ切面技术介绍与实现方式
2022-12-27 22:03:53
Java 数据结构与算法系列精讲之单向链表
2023-07-10 08:22:12
Spring boot配置绑定和配置属性校验的方式详解
2022-04-21 03:06:06
使用SpringBoot 工厂模式自动注入到Map
2021-12-22 10:02:42
Java 蒙特卡洛算法求圆周率近似值实例详解
2023-10-19 23:32:10
c#实现最简洁的快速排序(你绝对可以看懂)
2022-04-01 22:35:48
分布式系统下调用链追踪技术面试题
2023-11-25 05:55:00
详解Java中super的几种用法并与this的区别
2023-07-12 01:21:23
Java中线程状态+线程安全问题+synchronized的用法详解
2023-08-23 08:38:07
JAVA实现对阿里云DNS的解析管理
2023-11-24 13:25:15
java substring 截取字符串的方法
2023-02-12 17:21:19