Android[初级教程]第十三章 ProgressDialog控件
这次我们学习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:
&nbs
补充:移动开发 , Android ,