今天我們來介紹Progress Bar ,也就是我們一般常見的進度條。
Progress 官方提供了幾種形態的Style
Other progress bar styles provided by the system include:
Widget.ProgressBar.Horizontal
Widget.ProgressBar.Small
Widget.ProgressBar.Large
Widget.ProgressBar.Inverse
Widget.ProgressBar.Small.Inverse
Widget.ProgressBar.Large.Inverse
主要就是橫條、圓圈旋轉,在本範例中是以第一種來做練習。
對ProgressBar的操作方法:
getMax():取得這個進度條的範圍的上限
getProgress():取得主要進度
getSecondaryProgress():取得次要進度
incrementProgressBy(int):指定增加的進度(顯示條)
incrementSecondaryProgressBy(int):指定增加的進度(顯示條)
setProgress(int):設定主要進度(0-10000當主進度達到10000則Progress會淡出)
setSecondaryProgress(int):設定次要進度(0-10000)
isIndeterminate():指示進度條是否在不確定模式下
setIndeterminate(boolean):設置不確定模式下
setVisibility(int):設置該進度條是否可視
這一次的範例,主要會做兩個ProgressBar,
第一個Progress Bar 是透過Button來讓進度條前進,
其中一顆按鈕是讓SecondaryProgress(次要進度)進度增加,
另外的Button則是讓實際的Progress(主進度)進度增加。
而第二條ProgressBar則是要讓進度不斷的自動重複前進,
達到進度條持續跑的效果。
成果如下圖:
Layout XML如下:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.test.test1.ProgressActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_progress"/>
<ProgressBar
android:id="@+id/progressBar"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/pbutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button1"
android:text="SecondaryAdd" />
<Button
android:id="@+id/pbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button2"
android:text="incrementProgressAdd"/>
<ProgressBar
android:id="@+id/progressBar2"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
Activity程式如下:
package com.test.test1; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ProgressBar; public class ProgressActivity extends ActionBarActivity { ProgressBar pb; ProgressBar pbAuto; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress); pb = (ProgressBar) findViewById(R.id.progressBar); pbAuto = (ProgressBar) findViewById(R.id.progressBar2); setProgressBarVisibility(true); //====持續移動進度條 START==== // 設定Handler類別 final Handler handler = new Handler(); // 次要顯示處理程式 final Runnable callback1 = new Runnable() { @Override public void run() { pbAuto.incrementSecondaryProgressBy(5); setSecondaryProgress(pbAuto.getSecondaryProgress() * 100); } }; final Runnable callback2 = new Runnable() { @Override public void run() { pbAuto.incrementProgressBy(5); setProgress(pbAuto.getProgress() * 100); } }; // 建立執行序物件來執行Runnable Thread thread = new Thread() { @Override public void run() { // 讓進度條不斷自動跑 try { while (true) { // 當進度條滿後,將進度歸零 if (pbAuto.getProgress() == pbAuto.getMax()) { pbAuto.incrementSecondaryProgressBy(-100); pbAuto.incrementProgressBy(-100); } // 先跑SecondaryProgress handler.post(callback1); // 休息200 milliseconds Thread.sleep(200); // 在跑Progress handler.post(callback2); // 休息200 milliseconds Thread.sleep(200); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; thread.start(); //====持續移動進度條 END==== } // button1 function讓SecondaryProgress前進10% public void button1(View v) { pb.incrementSecondaryProgressBy(10); setSecondaryProgress(pb.getSecondaryProgress() * 100); } // button2 function讓Progress前進10% public void button2(View v) { pb.incrementProgressBy(5); setProgress(pb.getProgress() * 100); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.progress, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
留言列表