ProgressBar元件應用

1.UI的xml檔
<AbsoluteLayout xmlns: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=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="82dp"
        android:layout_x="34dp"
        android:layout_y="89dp"
        android:text="Start" />
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="156dp"
        android:layout_y="89dp"
        android:text="Start2" />
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="43dp"
        android:layout_x="68dp"
        android:layout_y="37dp"
        android:text="準備顯示進度對話框"
        android:textSize="20sp" />
</AbsoluteLayout>
2.主程式程式碼
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.app.ProgressDialog;

public class MainActivity extends Activity {
TextView tv;
Button btn, btn2;
ProgressDialog progresslog;
ProgressDialog progresslog2;
MyProgress myp;
MyProgress2 myp2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.text);
btn = (Button) this.findViewById(R.id.button1);
btn2 = (Button) this.findViewById(R.id.button2);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tv.setText("按返回鍵取消進度對話框");
progresslog = new ProgressDialog(MainActivity.this, 1);
progresslog.setMessage("Loading...");
progresslog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progresslog.show();
myp = new MyProgress(handler);
myp.start();
}
});
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tv.setText("按返回鍵取消進度對話框");
progresslog2 = new ProgressDialog(MainActivity.this, 1);
progresslog2.setMessage("Loading...");
progresslog2.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progresslog2.show();
myp2 = new MyProgress2(handler2);
myp2.start();
}

});
}
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int count = msg.arg1;
if (count == MyProgress.count) {
new MyProgress().setSate(MyProgress.sate, 0);
tv.setText("進度對話框完成");

}
}
};
final Handler handler2 = new Handler() {
public void handleMessage(Message msg) {
progresslog2.incrementProgressBy(10);
if (progresslog2.getProgress() == progresslog2.getMax()) {
tv.setText("進度對話框完成");
}
}
};
class MyProgress extends Thread {
private int total = 0;
private boolean limited = true;
public final static int count = 50;
public final static boolean sate = false;
Handler han;

MyProgress() {
}
MyProgress(Handler h) {
han = h;
}
public void run() {
while (limited) {
total++;
if (total == count) {
progresslog.dismiss();
}
Message msg = new Message();
msg.arg1 = total;
han.sendMessage(msg);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
progresslog.dismiss();
}
}
}
public void setSate(boolean sate, int total) {
limited = sate;
this.total = total;
}
}
class MyProgress2 extends Thread {
Handler han2;
MyProgress2(Handler h) {
han2 = h;
}
public void run() {
while (progresslog2.getProgress() <= progresslog2.getMax()) {
Message msg = new Message();
han2.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
progresslog2.dismiss();
}
if (progresslog2.getProgress() == progresslog2.getMax()) {
progresslog2.dismiss();
}
}
}
}
}

沒有留言: