国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發中ProgressDialog簡單用法示例

2019-10-22 18:25:23
字體:
來源:轉載
供稿:網友

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

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

建立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;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();      }    });  }}

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

 

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闻喜县| 苍南县| 太谷县| 寻甸| 遂昌县| 三门县| 津南区| 大田县| 玉龙| 工布江达县| 丰宁| 绥芬河市| 朝阳区| 武乡县| 秀山| 丹寨县| 云和县| 陆川县| 德昌县| 沈阳市| 姚安县| 宝坻区| 崇义县| 芷江| 绥滨县| 长白| 左云县| 五华县| 宜城市| 柳州市| 东方市| 哈巴河县| 南充市| 崇左市| 阜城县| 酉阳| 衢州市| 岱山县| 石家庄市| 林州市| 盘山县|