欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)中ProgressDialog簡單用法示例

 更新時間:2017年10月20日 11:31:41   作者:guochongcan  
這篇文章主要介紹了Android開發(fā)中ProgressDialog簡單用法,結(jié)合實例形式分析了Android使用ProgressDialog的進度條顯示與關閉、更新等事件響應相關操作技巧,需要的朋友可以參考下

本文實例講述了Android開發(fā)中ProgressDialog簡單用法。分享給大家供大家參考,具體如下:

網(wǎng)上一般對進度條的示例都是如何顯示,沒有在任務結(jié)束如何關閉的文章,參考其他文章經(jīng)過試驗之后把整套進度條顯示的簡單示例如下:

建立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發(fā)消息
            handler.sendEmptyMessage(0);
          }}.start();
      }
    });
  }
}

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發(fā)消息
            handler.sendEmptyMessage(0);
          }}.start();
      }
    });
  }
}

Calculation.java

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
    }
  }
}

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
    }
  }
}

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="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>

<?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收到消息中,它會把它放入到隊列中等待執(zhí)行,通常來說這會很快被執(zhí)行。

更多關于Android相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設計有所幫助。

相關文章

  • Android利用SoundPool實現(xiàn)音樂池

    Android利用SoundPool實現(xiàn)音樂池

    這篇文章主要為大家詳細介紹了Android利用SoundPool實現(xiàn)音樂池,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Android自定義流式布局/自動換行布局實例

    Android自定義流式布局/自動換行布局實例

    這篇文章主要介紹了Android自定義流式布局/自動換行布局實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android簡單實現(xiàn)app每月簽到功能

    Android簡單實現(xiàn)app每月簽到功能

    這篇文章主要為大家詳細介紹了Android簡單實現(xiàn)app每月簽到功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android 實現(xiàn)抖音頭像底部彈框效果的實例代碼

    Android 實現(xiàn)抖音頭像底部彈框效果的實例代碼

    這篇文章主要介紹了Android 實現(xiàn)抖音頭像底部彈框效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Android 畫一個太極圖實例代碼

    Android 畫一個太極圖實例代碼

    這篇文章主要介紹了Android 畫一個太極圖實例代碼的相關資料,需要的朋友可以參考下
    2016-09-09
  • Android編程之陰影(Shadow)制作方法

    Android編程之陰影(Shadow)制作方法

    這篇文章主要介紹了Android編程之陰影(Shadow)制作方法,結(jié)合實例形式分析了Android陰影效果實現(xiàn)函數(shù)setShadowLayer的具體使用技巧,需要的朋友可以參考下
    2016-10-10
  • Android中自定義進度條詳解

    Android中自定義進度條詳解

    這篇文章主要介紹了Android中自定義進度條詳解,本文講解了變換進度條前背景、縱向進度條、弧形bar等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼

    Android清空編輯框內(nèi)容功能的實現(xiàn)實例代碼

    本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實現(xiàn)實例代碼,非常具有實用價值,需要的朋友可以參考下。
    2017-03-03
  • Android Studio多渠道批量打包及代碼混淆

    Android Studio多渠道批量打包及代碼混淆

    這篇文章主要介紹了Android Studio多渠道批量打包及代碼混淆的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • Android 對Map按key和value分別排序的實例

    Android 對Map按key和value分別排序的實例

    下面小編就為大家?guī)硪黄狝ndroid 對Map按key和value分別排序的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12

最新評論