畫面載入顯示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" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="117dp"
        android:layout_y="62dp"
        android:text="" 
        android:textSize="30sp"/>
</AbsoluteLayout>
2.主程式程式碼
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity {
ProgressDialog progresslog;
MyProgressThread myp;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) this.findViewById(R.id.textView1);
setView();
}
public void setView() {
progresslog = new ProgressDialog(MainActivity.this, 1);
progresslog.setMessage("Loading...");
progresslog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progresslog.show();
myp = new MyProgressThread(handler);
myp.start();
}
class MyProgressThread extends Thread {
private int total = 0;
private boolean limited = true;
public final static int count = 50;
public final static boolean sate = false;
Handler han;
MyProgressThread() {
}
MyProgressThread(Handler han) {
this.han = han;
}
public void run() {
while (limited) {
total++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
progresslog.dismiss();
}
Message msg = new Message();
msg.arg1 = total;
han.sendMessage(msg);
}
}
public void setSate(boolean sate) {
limited = sate;
}
}
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
int count = msg.arg1;
if (count == MyProgressThread.count) {
progresslog.dismiss();
tv.setText("歡迎進入畫面");
new MyProgressThread().setSate(MyProgressThread.sate);
}
}
};
}

沒有留言: