Android[初級教程]第十三章 ProgressDialog控制項

來源:互聯網
上載者:User

這次我們學習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;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.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);}@Overridepublic void onClick(View v){//設定Handler對象,主要是處理新開線程完畢後交給主線程來處理的資料mhandler = new Handler(){@Overridepublic 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){//顯示ProgressDialogDialog.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,這一章也講完了,謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.