进度条的使用 ProgressBar类
布局文件:
<?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/ProgressBar01" style="?android:attr/progressBarStyleHorizontal" android:layout_width="200dp" android:layout_height="wrap_content" android:visibility="gone" ></ProgressBar> <ProgressBar android:id="@+id/ProgressBar02" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:max="100" android:progress="50" android:secondaryProgress="70" android:visibility="gone" ></ProgressBar> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始" ></Button> </LinearLayout> 代码: package yt.hy.progressbar; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; public class ProgressBarActivity extends Activity { private ProgressBar m_ProgressBar01; private ProgressBar m_ProgressBar02; private Button mbutton01; protected static final int GUI_STOP_NOTIFIER = 0x108; protected static final int GUI_THREADING_NOTIFIER = 0x109; public int intCounter = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置窗口模式,因为需要显示进度条在标题栏 requestWindowFeature(Window.FEATURE_PROGRESS); // 设置显示进度条 this.setProgressBarVisibility(true); setContentView(R.layout.main); // 取得ProgressBar m_ProgressBar01 = (ProgressBar)findViewById(R.id.ProgressBar01); m_ProgressBar02 = (ProgressBar)findViewById(R.id.ProgressBar02); mbutton01 = (Button)findViewById(R.id.Button01); *Change the indeterminate mode for this progress bar. In indeterminate mode, the progress is ignored and the progress bar shows an infinite animation instead. If this progress bar's style only supports indeterminate mode (such as the circular progress bars), then this will be ignored.*/ m_ProgressBar01.setIndeterminate(true); m_ProgressBar02.setIndeterminate(false); mbutton01.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // 设置为可见状态 m_ProgressBar01.setVisibility(View.VISIBLE); m_ProgressBar02.setVisibility(View.VISIBLE); // 设置ProgressBar的最大值 m_ProgressBar01.setMax(100); //设置当前值 m_ProgressBar01.setProgress(0); m_ProgressBar02.setProgress(0); // 通过线程改变ProgressBar的值 new Thread(new Runnable() { @Override public void run() { for (int i=0; i<10; i++) { try { intCounter = (i+1) * 20; Thread.sleep(1000); if (i == 4) { Message m = new Message(); m.what = ProgressBarActivity.GUI_STOP_NOTIFIER; // 给Handler发送消息 ProgressBarActivity.this.myMessageHandler.sendMessage(m); break; } else { Message m = new Message(); m.what = ProgressBarActivity.GUI_THREADING_NOTIFIER; // 给Handler发送消息 ProgressBarActivity.this.myMessageHandler.sendMessage(m); } } catch(Exception e) { e.printStackTrace(); } } } }).start(); } }); } Handler myMessageHandler = new Handler() { // 重写接收信息的方法 public void handleMessage(Message msg) { switch (msg.what) { // 进度已满 case GUI_STOP_NOTIFIER : m_ProgressBar01.setVisibility(View.GONE); m_ProgressBar02.setVisibility(View.GONE); // 打断线程 Thread.currentThread().interrupt(); break; case GUI_THREADING_NOTIFIER : if (!Thread.currentThread().isInterrupted()) { // 改变ProgressBar的当前值 m_ProgressBar01.setProgress(intCounter); m_ProgressBar02.setProgress(intCounter); // 设置标题栏中前景的进度条的进度值 setProgress(intCounter*100); // 设置标题栏中后景的进度条的进度值 setSecondaryProgress(intCounter*100 + 10*100); } break; } super.handleMessage(msg); } }; }
style="?android:attr/progressBarStyleHorizontal"设置进度条类型
android:visibility="gone"设置进度条不显示,用的时候再显示
android:progress="50"默认进度
android:secondaryProgress="70"底层的进度
m_ProgressBar01.setIndeterminate(false);
默认值为false。将此值设置为true可在进度条上呈现进度的文本显示。
设置进度条的 indeterminate 属性,该属性确定进度条处于确定模式中还是处于不确定模式中。不确定进度条连续地显示动画,指示发生未知长度的操作。默认情况下,此属性为 false。有些外观
可能不支持不确定进度条;它们将忽略此属性。 (也就是循序渐进的和跳进的模式)
<!--EndFragment-->