Android中ProgressDialog的簡單樣本

來源:互聯網
上載者:User

標籤:android



網上一般對進度條的樣本都是如何顯示,沒有在任務結束如何關閉的文章,參考其他文章經過實驗之後把整套進度條顯示的簡單樣本如下:

建立android工程等工作都略去,Google一下就可以了。

下面來介紹主要的Activity
ProgressBarDemo.java


package com.lveyo.android.demo.progressbar;

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.widget.Button;
import android.widget.TextView;

public class ProgressBarDemo extends Activity {
   
 private TextView statusTextView;
 private Button beginBtn;
 private ProgressDialog progressDialog;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        statusTextView = (TextView)findViewById(R.id.status);
        beginBtn = (Button)findViewById(R.id.beginBtn);
        setListener();
    }
   
    /**
     * 用Handler來更新UI
     */
    private Handler handler = new Handler(){

  @Override
  public void handleMessage(Message msg) {
   
   //關閉ProgressDialog
   progressDialog.dismiss();
   
   //更新UI
   statusTextView.setText("Completed!");
  }};
   
  
 /**
  * 點擊按鈕事件listener
  */
    private void setListener(){
     beginBtn.setOnClickListener(new View.OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    //顯示ProgressDialog
    progressDialog = ProgressDialog.show(ProgressBarDemo.this, "Loading...", "Please wait...", true, false);
    
    //建立線程
    new Thread(){

     @Override
     public void run() {
      //需要花時間計算的方法
      Calculation.calculate(4);
      
      //向handler發訊息
      handler.sendEmptyMessage(0);
     }}.start();
   }
  });
    }
   
}



package com.lveyo.android.demo.progressbar;

/**
 * 示意方法
 * @author lveyo
 *
 */
public class Calculation {
 
 public static void calculate(int sleepSeconds){
  try {
   Thread.sleep(sleepSeconds * 1000);
  } catch (Exception e) {
   // TODO: handle exception
  }
 }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/status"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<Button android:id="@+id/beginBtn"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="begin"
 />
</LinearLayout>


在android中,通常我們無法在單獨的線程中更新UI,而要在主線程中,這也就是為什麼我們要使用 Handler了,當handler收到訊息中,它會把它放入到隊列中等待執行,通常來說這會很快被執行。


 

Android中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.