您的位置:首页 > 移动开发 > Android开发

我的Android进阶之旅------>Android之进度条(ProgressBar)的功能和用法

2013-04-20 19:16 423 查看
一、简介

       进度条是UI界面中一种非常实用的组件,退出用于向用户显示某个比较耗时间的操作完成的百分比。因此进度条可以动态的显示进度,避免长时间地执行某个耗时操作,

让用户感觉程序失去了响应,从而更好的提高用户界面的友好性。

        Android支持几种风格的进度条,通过style属性可以为Progress指定风格。该属性可以支持如下几个属性值。



       除此之外,ProgressBar还支持下图所示常用的XML属性值。



还有一种进度条,它可以直接在窗口标题上显示,这种进度条甚至不需要使用ProgressBar组件,它直接由Activity的方法启用的。为了能在窗口标题上显示进度条,它需要如下两步。



二、通过一个小应用来学习一下ProgressBar

step1:新建一个项目MyProgressBar,如下图所示



step2:设计应用的UI界面

a./layout/main.xml对应MainActivity.java

<?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:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="任务完成的进度" />

<!-- 定义一个水平进度条 -->
<ProgressBar android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:id="@+id/bar"
style="@android:style/Widget.ProgressBar.Horizontal"/>

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="startTitleProgressBar"
android:text="start TitleProgressBar"
android:layout_gravity="bottom"/>
</LinearLayout>


b./layout/title.xml对应TitleProgressBar.java

<?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">

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/bn01"
android:text="显示"/>

<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/bn02"
android:text="隐藏" />
</LinearLayout>


step3:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.roco.progressbar" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<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>
<activity android:name=".TitleProgressBar" />
</application>
</manifest>


step4:MainActivity.java

package cn.roco.progressbar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends Activity {
//该程序模拟天成长度为100的数组
private int[] data = new int[100];

int hasData = 0;

// 记录ProgressBar的完成进度
int progressStatus = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final ProgressBar bar = (ProgressBar) findViewById(R.id.bar);
// 创建一个复杂更新进度的Handler
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x111) {
bar.setProgress(progressStatus);
}
}
};
// 启动线程来执行任务
new Thread() {
public void run() {
while (progressStatus < 100) {
// 获取耗时的完成百分比
progressStatus = doWork();
Message m = new Message();
m.what = 0x111;
// 发送消息到Handler
handler.sendMessage(m);
}
}
}.start();
}

//模拟一个耗时的操作
private int doWork() {
data[hasData++] = (int) (Math.random() * 100);
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
return hasData;
}

//对应main.xml中的button点击方法
public void startTitleProgressBar(View v){
Intent intent=new Intent(MainActivity.this,TitleProgressBar.class);
startActivity(intent);
}

}


step5:TitleProgressBar.java
package cn.roco.progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class TitleProgressBar extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//设置窗口特征:启动显示进度的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
//设置窗口特征:启动不显示进度的进度条
//		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.title);
Button bn1=(Button) findViewById(R.id.bn01);
Button bn2=(Button) findViewById(R.id.bn02);

bn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//显示带进度的进度条
setProgressBarVisibility(true);
//显示不带进度的进度条
//				setProgressBarIndeterminate(true);
//设置进度条的进度
setProgress(4500);
}
});

bn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//隐藏带进度的进度条
setProgressBarVisibility(false);
//隐藏不带进度的进度条
//				setProgressBarIndeterminate(false);
}
});

}
}


step6:运行效果


                      



                    



                        


==================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐