ProgressBar实现下载提示功能 博客分类: Android UI 界面 androidui progressbar 下载
在main.xml文件里面实现一个ProgressBar组件,把style设为水平horizontal (progressBar有两种显示方式),
同时记得水平铺满更加美观。
<?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" >
<ProgressBar
android:id="@+id/progressBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
/>
</LinearLayout>
第二步:
在Activity里面实现每隔两秒钟后进度条就加一个
a、定义成员变量
ProgressBar progressBar;
boolean flag=true;//如果满格了就结束睡眠
Handler handler;
b、初始化
init(){
progressBar=(ProgressBar) findViewById(R.id.progressBar1);
handler=new Handler();
}
c、必须要用handler,存储消息队列
handler。post(new Runnable(){
});
完整的代码如下
package com.lin; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.TextView; public class Day5ProgressBarActivity extends Activity { /** Called when the activity is first created. */ ProgressBar progressBar; boolean flag=true; Handler handler; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); new Thread(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub while(flag){ try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } handler.post(new Runnable(){ @Override public void run() { // TODO Auto-generated method stub if(progressBar.getProgress()==progressBar.getMax()){ ViewGroup group=(ViewGroup)progressBar.getParent(); if(group!=null){ group.removeView(progressBar); TextView textView=new TextView(Day5ProgressBarActivity.this); textView.setText("sucess"); group.addView(textView); } flag=false; } progressBar.incrementProgressBy(10); } }); } } }).start(); } private void init() { // TODO Auto-generated method stub progressBar=(ProgressBar) findViewById(R.id.progressBar1); handler=new Handler(); } }
效果如下:
1
2
3