android进度条ProgressBar例子

来源:互联网 发布:java se和jdk 编辑:程序博客网 时间:2024/04/28 15:05

android进度条ProgressBar例子

进行异步加载的时候,需要使用ProgressBar提示用户耐心等待。下面是ProgressBar使用的一个小例子。
布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ProgressBar        android:id="@+id/progressBar1"        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100"        android:layout_gravity="top"/>    <ProgressBar        android:id="@+id/progressbar2"        style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:max="100" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <ProgressBar            style="@android:style/Widget.ProgressBar.Large"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <ProgressBar            android:layout_width="wrap_content"            android:layout_height="wrap_content"></ProgressBar>        <ProgressBar            style="@android:style/Widget.ProgressBar.Small"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="任务完成进度" />    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="显示" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="隐藏" /></LinearLayout>

MainActivity.java
package com.example.wuxueyou.myfirstandroidapplication;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class MainActivity extends AppCompatActivity {    private static final String TAG = "MainActivity";    private ProgressBar bar1;    private ProgressBar bar2;    private Button button1;    private Button button2;    private int[] imgs = new int[]{            R.drawable.p1,            R.drawable.p2,            R.drawable.p3,    };    int flag = 0;    int status = 0;    Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if(msg.what == 0x123){                bar1.setProgress(status);                bar2.setProgress(status);            }        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_PROGRESS);        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        setContentView(R.layout.activity_main);        bar1 = (ProgressBar) this.findViewById(R.id.progressBar1);        bar2 = (ProgressBar) this.findViewById(R.id.progressbar2);        button1 = (Button)this.findViewById(R.id.button1);        button2 = (Button)this.findViewById(R.id.button2);        new Thread(){            public void run(){                while(status < 100){                    status = doWork();                    handler.sendEmptyMessage(0x123);                }            }        }.start();        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                bar1.setVisibility(View.VISIBLE);            }        });        button2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                bar1.setVisibility(View.GONE);            }        });    }    public int doWork(){        try {            Thread.sleep(100);        } catch (InterruptedException e) {            e.printStackTrace();        }        return ++status;    }}

运行效果:

0 0
原创粉丝点击