這次我們學習ProgressDialog控件,還是拿西遊記來說,唐僧被妖怪們抓去瞭,那悟空得去救啊,但妖怪肯定不讓啦,這就經過瞭一番打鬥,當然,妖怪肯定打不過悟空啦,我們就用ProgressDialog來模擬打妖怪的過程,設定為100隻妖怪,打完這100隻妖怪才能救出師傅.看圖:
呵呵,這次悟空沒出手,讓八戒跟沙僧搶瞭回頭功,來看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="match_parent"
android:layout_height="match_parent">
<Button android:text="悟空去救師傅" android:id="@+id/wukong"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="八戒去救師傅" android:id="@+id/bajie"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="沙僧去救師傅" android:id="@+id/shaseng"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
還是老樣子,定義瞭幾個按鈕,接下來看Activity的java源碼:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ProgressDialogDemo extends Activity implements OnClickListener
{
private ProgressDialog Dialog;
private Handler mhandler;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progressdialog);
Button wukong = (Button) findViewById(R.id.wukong);
wukong.setOnClickListener(this);
Button bajie = (Button) findViewById(R.id.bajie);
bajie.setOnClickListener(this);
Button shaseng = (Button) findViewById(R.id.shaseng);
shaseng.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
//設定Handler對象,主要是處理新開線程完畢後交給主線程來處理的數據
mhandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
String name =(String)msg.obj;
Toast.makeText(ProgressDialogDemo.this, name + "把師傅救出來瞭", 1).show();
}
};
//創建ProgressDialog對象
Dialog = new ProgressDialog(this);
//設定ProgressDialog的樣式為進度條
Dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設定ProgressDialog的最大值為100,這裡就是100隻小妖怪啦
Dialog.setMax(100);
//設定ProgressDialog不能取消,你不能半途而廢啊,當然要100隻打完啦
Dialog.setCancelable(false);
String name = null;
switch (v.getId())
{
case R.id.wukong:
//設定名字,看是誰在打妖怪啊
name = "孫悟空";
Dialog.setTitle(name);
//圖片
Dialog.setIcon(R.drawable.wukong);
//消息
Dialog.setMessage("悟空在打妖怪");
//自定義打鬥的方法
doFlight(name);
break;
case R.id.bajie:
//同上
name = "豬八戒";
Dialog.setTitle(name);
Dialog.setIcon(R.drawable.bajie);
Dialog.setMessage("八戒在打妖怪");
doFlight(name);
break;
case R.id.shaseng:
//同上
name = "沙和尚";
Dialog.setTitle(name);
Dialog.setIcon(R.drawable.shaseng);
Dialog.setMessage("沙僧在打妖怪");
doFlight(name);
break;
}
}
private void doFlight(final String name)
{
//顯示ProgressDialog
Dialog.show();
//新開一條線程
new Thread()
{
//打完妖怪的數量
int count = 0;
public void run()
{
try
{
//打完妖怪小於100隻的時候運行的方法
while(count <= 100){
Dialog.setProgress(count++);
//睡眠0.2秒,你也得讓他們休息一下啊,呵呵
Thread.sleep(200);
}
Dialog.cancel();
//給handler發送消息,看是誰在打妖怪,handler是主線程中的
Message message = new Message();
message.obj = name;
mhandler.sendMessage(message);
} catch (InterruptedException e)
{
Dialog.cancel();
}
};
}.start();
}
}
這裡面涉及瞭子線程和主線程的通信,通過Handler可以將子線程運行的數據最終交給主線程,線程這一章我們會在接下來講,OK,這一章也講完瞭,謝謝
摘自:kangkangz4的專欄