PBar.java
package com.example.progressbar; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class PBar extends Activity { private ProgressBar firstBar = null; private ProgressBar secondBar = null; private Button B = null; private int i = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pbar); firstBar = (ProgressBar) findViewById(R.id.firstBar); secondBar = (ProgressBar) findViewById(R.id.secondBar); B = (Button) findViewById(R.id.myButton); B.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(i==0){ firstBar.setVisibility(View.VISIBLE); secondBar.setVisibility(View.VISIBLE); } else if(i<firstBar.getMax()){ firstBar.setProgress(i); //设置主进度条(深色) 播放量 firstBar.setSecondaryProgress(i+10); //设置次进度条(浅色)缓冲量 //secondBar.setProgress(i); 默认进度条不会显示进度 } else { firstBar.setVisibility(View.GONE); secondBar.setVisibility(View.GONE); } i+=50; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_pbar, menu); return true; } }
xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="@string/hello" /> 11 <!-- 12 设置进度条类型 13 style="?android:attr/progressBarStyleHorizontal" 14 设置进度条不可见 15 android:visibility="gone" 16 设置进度条MAX 17 android:max="500" 18 --> 19 20 <ProgressBar 21 android:id="@+id/firstBar" 22 style="?android:attr/progressBarStyleHorizontal" 23 android:layout_width="200dp" 24 android:layout_height="wrap_content" 25 android:max="500" 26 android:visibility="gone" /> 27 28 <ProgressBar 29 android:id="@+id/secondBar" 30 style="?android:attr/progressBarStyle" 31 android:layout_width="wrap_content" 32 android:layout_height="wrap_content" 33 android:visibility="gone" /> 34 35 <Button 36 android:id="@+id/myButton" 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:text="begin" /> 40 41 </LinearLayout>