ProgressDialog的簡單應用,等待提示

來源:互聯網
上載者:User

在應用中經常會用到一些費時的操作,需要使用者進行等待,比如載入網頁內容……

這時候就需要一個提示來告訴使用者程式正在執行,並沒有假死或者真死……囧……

而ProgressBar、ProgressDialog等就是專門幹這個的。


以ProgressDialog為例,一般的使用它步驟為:在執行耗時間的操作之前彈出ProgressDialog提示使用者,然後開一個新線程,在新線程裡執行耗時的操作,執行完畢之後通知主程式將ProgressDialog結束。

以下是一個demo,很簡單的用法:

[java]
view plaincopyprint?
  1. package com.android.ui;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.ProgressDialog;  
  5. import android.os.Bundle;  
  6. import android.os.Handler;  
  7. import android.os.Message;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private Button button;  
  15.     private ProgressDialog pd;  
  16.   
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.main);  
  21.   
  22.         button = (Button) findViewById(R.id.button1);  
  23.   
  24.         button.setOnClickListener(new OnClickListener() {  
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 /* 顯示ProgressDialog */  
  28.                 pd = ProgressDialog.show(MainActivity.this, "標題", "載入中,請稍後……");  
  29.   
  30.                 /* 開啟一個新線程,在新線程裡執行耗時的方法 */  
  31.                 new Thread(new Runnable() {  
  32.                     @Override  
  33.                     public void run() {  
  34.                         spandTimeMethod();// 耗時的方法  
  35.                         handler.sendEmptyMessage(0);// 執行耗時的方法之後發送消給handler  
  36.                     }  
  37.   
  38.                 }).start();  
  39.             }  
  40.         });  
  41.   
  42.     }  
  43.   
  44.     private void spandTimeMethod() {  
  45.         try {  
  46.             Thread.sleep(5000);  
  47.         } catch (InterruptedException e) {  
  48.             // TODO Auto-generated catch block  
  49.             e.printStackTrace();  
  50.         }  
  51.     }  
  52.   
  53.     Handler handler = new Handler() {  
  54.         @Override  
  55.         public void handleMessage(Message msg) {// handler接收到訊息後就會執行此方法  
  56.             pd.dismiss();// 關閉ProgressDialog  
  57.         }  
  58.     };  
  59. }  

main.xml中只有一個Button,就不貼了,程式應該很好理解,點擊按鈕後彈出ProgressDialog,在新線程中執行耗時操作(Thread.sleep(5000);),執行完畢之後通知handler,結束ProgressDialog。

運行效果如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.