上一篇《ProgressBar和ProgressDialog(一)》
三、ProgressBar使用实例
以下一个是ProgressBar的实例,由于ProgressBar的基本使用很简单,我就不对实例做详细介绍了。
主要界面,截图5
点击"download"按钮, 截图6
ProgressBarDemoActivity.java文件
package com.teleca.robin;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class
ProgressBarDemoActivity
extends
Activity {
boolean blCanced = false;
final Handler handler = new Handler();
private int mProgressStatus = 0;
final static String tag = "robin";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void
onClickDownLoad
(View view) {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.ProgressBar01);
final TextView numberText = (TextView) findViewById(R.id.progress_number);
// initializing progress when user re-enter
progressBar.setProgress(0);
numberText.setText("(" + progressBar.getProgress() + "/"
+ progressBar.getMax() + ")");
final Button btn = (Button) view;
btn.setClickable(false);
mProgressStatus = 0;
final int progressBarMax = progressBar.getMax();
final Context context = this;
final Toast toast = Toast.makeText(context, "downloading is finished",
Toast.LENGTH_LONG);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void
run()
{
while (mProgressStatus < progressBarMax) {
mProgressStatus = doWork();
// Update the progress bar
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(mProgressStatus);
numberText.setText("(" + progressBar.getProgress()
+ "/" + progressBarMax + ")");
if (mProgressStatus >= progressBarMax) {
btn.setText("download again");
btn.setClickable(true);
toast.show();
}
}
});
}
}
}).start();
}
public void
onClickPlay
(View view) {
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.ProgressBar02);
final TextView numberText = (TextView) findViewById(R.id.progress_number02);
// initializing progress when user re-enter
mProgressStatus = 0;
mSecondProgressStatus=0;
progressBar.setProgress(0);
progressBar.setSecondaryProgress(0);
numberText.setText("(" + progressBar.getProgress() + "/"
+ progressBar.getMax() + ")");
final Button btn = (Button) view;
btn.setClickable(false);
final int progressBarMax = progressBar.getMax();
final Context context = this;
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < progressBarMax) {
doPlayWork();
Log.i(tag, "do working" + mProgressStatus);
// Update the progress bar
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(mProgressStatus);
int playPercent = progressBar.getProgress() * 100
/ progressBarMax;
int cachedPercent = progressBar
.getSecondaryProgress()
* 100
/ progressBarMax;
numberText.setText("play " + playPercent + "%"
+ " cached " + cachedPercent + "%");
if (mProgressStatus >= progressBarMax) {
btn.setText("replay");
btn.setClickable(true);
}
}
});
}
}
}).start();
new Thread(new Runnable() {
public void
run()
{
while (mSecondProgressStatus < progressBarMax) {
doCacheWork();
Log.i(tag, "do cache working" + mSecondProgressStatus);
// Update the progress bar
handler.post(new Runnable() {
public void run() {
progressBar
.setSecondaryProgress(mSecondProgressStatus);
int playPercent = progressBar.getProgress() * 100
/ progressBarMax;
int cachedPercent = progressBar
.getSecondaryProgress()
* 100
/ progressBarMax;
numberText.setText("play " + playPercent + "%"
+ " cached " + cachedPercent + "%");
}
});
}
}
}).start();
}
int
doWork
() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mProgressStatus = mProgressStatus + 1;
return
mProgressStatus;
}
void
doPlayWork
() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mProgressStatus = mProgressStatus + 1;
}
int mSecondProgressStatus = 0;
void
doCacheWork
() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mSecondProgressStatus = mSecondProgressStatus + 1;
}
public void
onClickGoProgressDialog
(View view)
{
Intent intent=new Intent(this,ProgressDialogSampleActivity.class);
startActivity(intent);
}
}
main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Horizontal" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<
ProgressBar
android:id="@+id/ProgressBar01"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/progress_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickDownLoad"
android:text="download" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Horizontal" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<
ProgressBar
android:id="@+id/ProgressBar02"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="150dp"
android:layout_height="40dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="@+id/progress_number02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickPlay"
android:text="play" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="indeterminate Horizontal" />
<
ProgressBar
android:id="@+id/ProgressBar03"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:indeterminate="true
" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Small" />
<
ProgressBar
android:id="@+id/ProgressBar04"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large" />
<
ProgressBar
android:id="@+id/ProgressBar05"
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Inverse" />
<
ProgressBar
android:id="@+id/ProgressBar06"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Small.Inverse" />
<
ProgressBar
android:id="@+id/ProgressBar07"
style="@android:style/Widget.ProgressBar.Small.Inverse"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large.Inverse" />
<
ProgressBar
android:id="@+id/ProgressBar08"
style="@android:style/Widget.ProgressBar.Large.Inverse"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickGoProgressDialog"
android:text="go ProgressDialog sample list" />
</LinearLayout>
strings.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, ProgressBarDemoActivity!</string>
<string name="app_name">ProgressBarDemo</string>
</resources>
AndroidManifest.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teleca.robin"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name="
.ProgressBarDemoActivity
" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name="
.ProgressDialogSampleActivity
" >
<intent-filter >
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>