標籤:
一、代碼
1.xml
(1)main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello"11 />12 <ProgressBar13 android:id="@+id/firstBar"14 style="?android:attr/progressBarStyleHorizontal"15 android:layout_width="200dp"16 android:layout_height="wrap_content"17 android:visibility="gone"18 />19 <ProgressBar20 android:id="@+id/secondBar"21 style="?android:attr/progressBarStyle"22 android:layout_width="wrap_content"23 android:layout_height="wrap_content"24 android:visibility="gone"25 />26 <Button27 android:id="@+id/myButton"28 android:layout_width="wrap_content"29 android:layout_height="wrap_content"30 android:text="begin"31 />32 </LinearLayout>
2.java
(1)ProgressBarTest.java
1 package mars.progressbar; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.ProgressBar; 9 10 public class ProgressBarTest extends Activity {11 /** Called when the activity is first created. */12 //聲明變數13 private ProgressBar firstBar =null;14 private ProgressBar secondBar = null;15 private Button myButton = null;16 private int i = 0 ;17 @Override18 public void onCreate(Bundle savedInstanceState) {19 super.onCreate(savedInstanceState);20 setContentView(R.layout.main);21 //根據控制項的ID來取得代表控制項的對象22 firstBar = (ProgressBar)findViewById(R.id.firstBar);23 secondBar = (ProgressBar)findViewById(R.id.secondBar);24 myButton = (Button)findViewById(R.id.myButton);25 myButton.setOnClickListener(new ButtonListener());26 }27 class ButtonListener implements OnClickListener{28 29 @Override30 public void onClick(View v) {31 if(i == 0)32 {33 //設定進度條處於可見的狀態34 firstBar.setVisibility(View.VISIBLE);35 firstBar.setMax(150);36 secondBar.setVisibility(View.VISIBLE);37 }38 else if ( i < firstBar.getMax()){39 //設定主進度條的當前值40 firstBar.setProgress(i);41 //設定第二進度條的當前值42 firstBar.setSecondaryProgress(i + 10);43 //因為預設的進度條無法顯示進行的狀態44 //secondBar.setProgress(i);45 46 }47 else{48 //設定進度條處於不可見狀態49 firstBar.setVisibility(View.GONE);50 secondBar.setVisibility(View.GONE);51 }52 i = i + 10 ;53 }54 55 }56 57 }
ANDROID_MARS學習筆記_S01原始版_005_ProgressBar