- Android基础
- Android - 首页
- Android - 概述
- Android - 环境搭建
- Android - 架构
- Android - 应用组件
- Android - Hello World示例
- Android - 资源
- Android - 活动 (Activities)
- Android - 服务 (Services)
- Android - 广播接收器 (Broadcast Receivers)
- Android - 内容提供商 (Content Providers)
- Android - 片段 (Fragments)
- Android - 意图/过滤器 (Intents/Filters)
- Android - 用户界面
- Android - UI布局
- Android - UI控件
- Android - 事件处理
- Android - 样式和主题
- Android - 自定义组件
- Android高级概念
- Android - 拖放
- Android - 通知
- 基于位置的服务
- Android - 发送电子邮件
- Android - 发送短信
- Android - 电话呼叫
- 发布Android应用程序
- Android实用示例
- Android - 警报对话框
- Android - 动画
- Android - 音频捕获
- Android - AudioManager
- Android - 自动完成
- Android - 最佳实践
- Android - 蓝牙
- Android - 相机
- Android - 剪贴板
- Android - 自定义字体
- Android - 数据备份
- Android - 开发者工具
- Android - 模拟器
- Android - Facebook集成
- Android - 手势
- Android - Google地图
- Android - 图片特效
- Android - ImageSwitcher
- Android - 内部存储
- Android - JetPlayer
- Android - JSON解析器
- Android - Linkedin集成
- Android - 加载旋转器 (Loading Spinner)
- Android - 本地化
- Android - 登录界面
- Android - MediaPlayer
- Android - 多点触控
- Android - 导航
- Android - 网络连接
- Android - NFC指南
- Android - PHP/MySQL
- Android - 循环进度条 (Progress Circle)
- Android - 进度条 (ProgressBar)
- Android - 推送通知
- Android - RenderScript
- Android - RSS阅读器
- Android - 屏幕录制
- Android - SDK管理器
- Android - 传感器
- Android - 会话管理
- Android - 共享首选项
- Android - SIP协议
- Android - 拼写检查器
- Android - SQLite数据库
- Android - 支持库
- Android - 测试
- Android - 文字转语音
- Android - TextureView
- Android - Twitter集成
- Android - UI设计
- Android - UI模式
- Android - UI测试
- Android - WebView布局
- Android - Wi-Fi
- Android - 小部件 (Widgets)
- Android - XML解析器
- Android实用资源
- Android - 问答
- Android - 实用资源
- Android - 讨论
使用ProgressDialog的Android进度条
进度条用于显示任务的进度。例如,当您从互联网上传下载某些内容时,最好向用户显示下载/上传进度。
在Android中,有一个名为ProgressDialog的类允许您创建进度条。为此,您需要实例化此类的对象。其语法如下。
ProgressDialog progress = new ProgressDialog(this);
现在您可以设置此对话框的一些属性。例如,它的样式、文本等。
progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
除了这些方法之外,ProgressDialog类还提供了其他方法。
| 序号 | 标题和描述 |
|---|---|
| 1 |
getMax() 此方法返回进度的最大值。 |
| 2 |
incrementProgressBy(int diff) 此方法将进度条增加传递的参数值差。 |
| 3 |
setIndeterminate(boolean indeterminate) 此方法将进度指示器设置为确定性或不确定性。 |
| 4 |
setMax(int max) 此方法设置进度对话框的最大值。 |
| 5 |
setProgress(int value) 此方法用于使用特定值更新进度对话框。 |
| 6 |
show(Context context, CharSequence title, CharSequence message) 这是一个静态方法,用于显示进度对话框。 |
示例
此示例演示了进度对话框(实际上是进度条)的水平使用。它在按下按钮时显示进度条。
要试验此示例,您需要在按照以下步骤开发应用程序后,在一个实际设备上运行它。
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用Android Studio在com.example.sairamkrishna.myapplication包下创建一个Android应用程序。 |
| 2 | 修改src/MainActivity.java文件以添加显示进度对话框的进度代码。 |
| 3 | 修改res/layout/activity_main.xml文件以添加相应的XML代码。 |
| 4 | 运行应用程序,选择正在运行的Android设备,并在其上安装应用程序,然后验证结果。 |
以下是修改后的主活动文件src/MainActivity.java的内容。
package com.example.sairamkrishna.myapplication;
import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button b1;
private ProgressDialog progress;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button2);
}
public void download(View view){
progress=new ProgressDialog(this);
progress.setMessage("Downloading Music");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while(jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();
}
}
将res/layout/activity_main.xml的内容修改为以下内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 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" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Progress bar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="@+id/textView2"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download"
android:onClick="download"
android:id="@+id/button2"
android:layout_marginLeft="125dp"
android:layout_marginStart="125dp"
android:layout_centerVertical="true" />
</RelativeLayout>
这是默认的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
让我们尝试运行您的应用程序。我们假设您已将实际的Android移动设备连接到计算机。要从Android Studio运行应用程序,请打开项目的其中一个活动文件,然后单击工具栏中的运行
图标。在启动应用程序之前,Android Studio将显示以下窗口,供您选择要在其中运行Android应用程序的选项。
选择您的移动设备作为选项,然后检查您的移动设备,它将显示以下屏幕:
只需按下按钮即可启动进度条。按下后,将出现以下屏幕:
它将不断更新自身。
