您好,欢迎访问一九零五行业门户网

Android UI控件系列:ProgressBar(进度条)

android在执行一些后台操作的时候,比如加载游戏,播放歌曲时,用户根本不知道程序执行的进度情况,这时候,可以使用进度条来显示这些进度。
andorid系统提供两种进度条,长条形进度条(progressbarstylehorizontal)和圆形进度条(progressbarstylelarge),android平台默认的进度条是第二种。另外,还可以在窗体的标题栏设置进度条,这就需要先对窗体的显示风格进行设置“requestwindowfeature(window.feature_progress)”;如果要显示这个进度条,还要使用setprogressbarvisibility(true);方法来使其显示
下面是个例子,分别由标题栏进度条,长条形进度条和圆形进度条组成,并用线程控制。
注意:圆形进度条是不会显示具体的进度的,而只是单纯的旋转
package org.hualang.progress; 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 progressbartest extends activity { //声明progressbar对象 private progressbar pro1; private progressbar pro2; private button btn; protected static final int stop_notifier = 000; protected static final int threading_notifier = 111; public int intcounter=0; /** called when the activity is first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //设置窗口模式,,因为需要显示进度条在标题栏 requestwindowfeature(window.feature_progress); //设置标题栏上的进度条可见 setprogressbarvisibility(true); setcontentview(r.layout.main); //取得progressbar pro1 = (progressbar) findviewbyid(r.id.progress1); pro2= (progressbar) findviewbyid(r.id.progress2); btn = (button)findviewbyid(r.id.button); //设置进度条是否自动运转,即设置其不确定模式,false表是不自动运转 pro1.setindeterminate(false); pro2.setindeterminate(false); //当按钮按下时开始执行, btn.setonclicklistener(new button.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub //设置progressbar为可见状态 pro1.setvisibility(view.visible); pro2.setvisibility(view.visible); //设置progressbar的最大值 pro1.setmax(100); //设置progressbar当前值 pro1.setprogress(0); pro2.setprogress(0); //通过线程来改变progressbar的值 new thread(new runnable() { public void run() { for (int i = 0; i < 10; i++) { try { intcounter = (i + 1) * 20; thread.sleep(1000); if (i == 4) { message m = new message(); m.what = progressbartest.stop_notifier; progressbartest.this.mymessagehandler.sendmessage(m); break; } else { message m = new message(); m.what = progressbartest.threading_notifier; progressbartest.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 progressbartest.stop_notifier: pro1.setvisibility(view.gone); pro2.setvisibility(view.gone); thread.currentthread().interrupt(); break; case progressbartest.threading_notifier: if (!thread.currentthread().isinterrupted()) { // 改变progressbar的当前值 pro1.setprogress(intcounter); pro2.setprogress(intcounter); // 设置标题栏中前景的一个进度条进度值 setprogress(intcounter*100); // 设置标题栏中后面的一个进度条进度值 setsecondaryprogress(intcounter*100);// } break; } super.handlemessage(msg); } }; }
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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进度条实例演示" /> <progressbar android:id="@+id/progress1" style="?android:attr/progressbarstylehorizontal"---设置进度条为长条形 android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone"---设置其不可见 /> <progressbar android:id="@+id/progress2" 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/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="启动进度条" /> </linearlayout>
运行结果如下:
点击启动进度条按钮后,进度条将自动运行
当结束时,自动退出进度条程序,返回第一个图片的样子
以上就是android ui控件系列:progressbar(进度条)的内容。
其它类似信息

推荐信息