当一个应用在后台执行时,前台界面就不会有什么信息,这时用户根本不知道程序是否在执行、执行进度如何、应用程序是否遇到错误终止等,这时需要使用进度条来提示用户后台程序执行的进度。Android系统提供了两大类进度条样式,长形进度条(progress-BarStyleHorizontal)
和圆形进度条(progressBarStyleLarge)。进度条用处很多,比如,应用程序装载资源和网络连接时,可以提示用户稍等,这一类进度条只是代表应用程序中某一部分的执行情况,而整个应用程序执行情况呢,则可以通过应用程序标题栏来显示一个进度条,这就需要先对窗口的显示风格进行设置"requestWindowFeature(Window.FEATURE_PROGRESS)"。
先上布局文件
<?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="@string/hello"
/>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/ProgressBar02"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:visibility="gone"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
</LinearLayout>
ProgressBar01 为长形进度条,Progress02为圆形进度条,标题栏进度条没有在这里声明。"style="表示设置进度条的风格。下面通过setMax方法设置进度条的最大值,setProgress 方法设置进度条的当前值,setVisibility 方法设置进度条的可见性。
Java代码
package xiaohang.zhimeng;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class Activity01 extends Activity {
//声明ProgressBar对象
private ProgressBar xh_ProgressBar;
private ProgressBar xh_ProgressBar2;
private Button xh_Button;
protected static final int GUI_STOP_NOTIFIER = 0x108;
protected static final int GUI_THREADING_NOTIFIER = 0x109;
public int intCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口模式,因为需要显示进度条在标题栏
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.main);
//得到ProgressBar
xh_ProgressBar = (ProgressBar) findViewById(R.id.ProgressBar01);
xh_ProgressBar2 = (ProgressBar) findViewById(R.id.ProgressBar02);
xh_Button = (Button) findViewById(R.id.Button01);
//设置进度条 不为 不确定模式
xh_ProgressBar.setIndeterminate(false);
xh_ProgressBar2.setIndeterminate(false);
//当按钮按下时开始执行
xh_Button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//设置ProgressBar为可见状态
xh_ProgressBar.setVisibility(View.VISIBLE);
xh_ProgressBar2.setVisibility(View.VISIBLE);
//设置ProgressBar的最大值
xh_ProgressBar.setMax(100);
//设置ProgressBar当前值
xh_ProgressBar.setProgress(0);
xh_ProgressBar2.setProgress(0);
//通过线程来改变ProgressBar的值
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
//设置进度值
intCounter = (i + 1) * 20;
//睡眠1000毫秒
Thread.sleep(1000);
if (i == 4) {
Message m = new Message();
m.what = Activity01.GUI_STOP_NOTIFIER;
Activity01.this.myMessageHandler
.sendMessage(m);
break;
} else {
Message m = new Message();
m.what = Activity01.GUI_THREADING_NOTIFIER;
Activity01.this.myMessageHandler
.sendMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
Handler myMessageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//ProgressBar已经是最大值
case Activity01.GUI_STOP_NOTIFIER:
xh_ProgressBar.setVisibility(View.GONE);
xh_ProgressBar2.setVisibility(View.GONE);
Thread.currentThread().interrupted();
break;
case Activity01.GUI_THREADING_NOTIFIER:
if (!Thread.currentThread().isInterrupted()) {
//改变ProgressBar的当前值
xh_ProgressBar.setProgress(intCounter);
xh_ProgressBar2.setProgress(intCounter);
//设置标题栏中前景的一个进度条进度值
setProgress(intCounter * 100);
}
break;
}
super.handleMessage(msg);
}
};
}
和圆形进度条(progressBarStyleLarge)。进度条用处很多,比如,应用程序装载资源和网络连接时,可以提示用户稍等,这一类进度条只是代表应用程序中某一部分的执行情况,而整个应用程序执行情况呢,则可以通过应用程序标题栏来显示一个进度条,这就需要先对窗口的显示风格进行设置"requestWindowFeature(Window.FEATURE_PROGRESS)"。
先上布局文件
<?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="@string/hello"
/>
<ProgressBar
android:id="@+id/ProgressBar01"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/ProgressBar02"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:progress="50"
android:secondaryProgress="70"
android:visibility="gone"
/>
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
</LinearLayout>
ProgressBar01 为长形进度条,Progress02为圆形进度条,标题栏进度条没有在这里声明。"style="表示设置进度条的风格。下面通过setMax方法设置进度条的最大值,setProgress 方法设置进度条的当前值,setVisibility 方法设置进度条的可见性。
Java代码
package xiaohang.zhimeng;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
public class Activity01 extends Activity {
//声明ProgressBar对象
private ProgressBar xh_ProgressBar;
private ProgressBar xh_ProgressBar2;
private Button xh_Button;
protected static final int GUI_STOP_NOTIFIER = 0x108;
protected static final int GUI_THREADING_NOTIFIER = 0x109;
public int intCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口模式,因为需要显示进度条在标题栏
requestWindowFeature(Window.FEATURE_PROGRESS);
setProgressBarVisibility(true);
setContentView(R.layout.main);
//得到ProgressBar
xh_ProgressBar = (ProgressBar) findViewById(R.id.ProgressBar01);
xh_ProgressBar2 = (ProgressBar) findViewById(R.id.ProgressBar02);
xh_Button = (Button) findViewById(R.id.Button01);
//设置进度条 不为 不确定模式
xh_ProgressBar.setIndeterminate(false);
xh_ProgressBar2.setIndeterminate(false);
//当按钮按下时开始执行
xh_Button.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
//设置ProgressBar为可见状态
xh_ProgressBar.setVisibility(View.VISIBLE);
xh_ProgressBar2.setVisibility(View.VISIBLE);
//设置ProgressBar的最大值
xh_ProgressBar.setMax(100);
//设置ProgressBar当前值
xh_ProgressBar.setProgress(0);
xh_ProgressBar2.setProgress(0);
//通过线程来改变ProgressBar的值
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
//设置进度值
intCounter = (i + 1) * 20;
//睡眠1000毫秒
Thread.sleep(1000);
if (i == 4) {
Message m = new Message();
m.what = Activity01.GUI_STOP_NOTIFIER;
Activity01.this.myMessageHandler
.sendMessage(m);
break;
} else {
Message m = new Message();
m.what = Activity01.GUI_THREADING_NOTIFIER;
Activity01.this.myMessageHandler
.sendMessage(m);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
Handler myMessageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//ProgressBar已经是最大值
case Activity01.GUI_STOP_NOTIFIER:
xh_ProgressBar.setVisibility(View.GONE);
xh_ProgressBar2.setVisibility(View.GONE);
Thread.currentThread().interrupted();
break;
case Activity01.GUI_THREADING_NOTIFIER:
if (!Thread.currentThread().isInterrupted()) {
//改变ProgressBar的当前值
xh_ProgressBar.setProgress(intCounter);
xh_ProgressBar2.setProgress(intCounter);
//设置标题栏中前景的一个进度条进度值
setProgress(intCounter * 100);
}
break;
}
super.handleMessage(msg);
}
};
}