这次我们学习ProgressDialog控件,还是拿西游记来说,唐僧被妖怪们抓去了,那悟空得去救啊,但妖怪肯定不让啦,这就经过了一番打斗,当然,妖怪肯定打不过悟空啦,我们就用ProgressDialog来模拟打妖怪的过程,设定为100只妖怪,打完这100只妖怪才能救出师傅.看图:
![Android[初级教程]第十三章 ProgressDialog控件 Android[初级教程]第十三章 ProgressDialog控件](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzc5NC80ZjhjNTNkYmU2ZjlkZDQ1ZjYyMzc0M2E5MDM5NzRhMi5KUEVH)
![Android[初级教程]第十三章 ProgressDialog控件 Android[初级教程]第十三章 ProgressDialog控件](/default/index/img?u=aHR0cHM6Ly9waWFuc2hlbi5jb20vaW1hZ2VzLzQzNC9lNDM4YzY5ODc4NDhhMDliNmQxYTJkN2ZhYmMxNDc2YS5KUEVH)
呵呵,这次悟空没出手,让八戒跟沙僧抢了回头功,来看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,这一章也讲完了,谢谢