首先还是ProgressBarTest.java主activity的内容:
package txlong.ProgressBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
public class ProgressBarTest extends Activity {
private ProgressBar psb1, psb2;
private Button btn;
private int flag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
psb1 = (ProgressBar) findViewById(R.id.psb1);
psb2 = (ProgressBar) findViewById(R.id.psb2);
btn = (Button) findViewById(R.id.btn);
psb1.setMax(100);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (flag == 0) {
psb1.setProgress(0);
psb1.setSecondaryProgress(0);
psb1.setVisibility(View.VISIBLE);
psb2.setVisibility(View.VISIBLE);
} else if (flag < psb1.getMax()) {
psb1.setProgress(flag);
psb1.setSecondaryProgress(flag + 10);
} else {
psb1.setVisibility(View.GONE);
psb2.setVisibility(View.GONE);
flag = -10;
}
flag += 10;
}
});
}
}
然后是main.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello"/> <ProgressBar android:id="@+id/psb1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone"/> <ProgressBar android:id="@+id/psb2" style="?android:attr/progressBarStyle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click"/> </LinearLayout>