Android-進階UI控制項_ProgressDialog進度條對話方塊

來源:互聯網
上載者:User

標籤:

代碼

package com.lxt008;import android.app.Activity;import android.app.ProgressDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.widget.Button;public class Activity01 extends Activity{    private Button mButton01,mButton02;        int m_count = 0;    //聲明進度條對話方塊    ProgressDialog m_pDialog;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //得到按鈕對象        mButton01 = (Button)findViewById(R.id.Button01);        mButton02 = (Button)findViewById(R.id.Button02);                //設定mButton01的事件監聽        mButton01.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v)            {                // TODO Auto-generated method stub                                //建立ProgressDialog對象                m_pDialog = new ProgressDialog(Activity01.this);                // 設定進度條風格,風格為圓形,旋轉的                m_pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);                // 設定ProgressDialog 標題                m_pDialog.setTitle("提示");                                // 設定ProgressDialog 提示資訊                m_pDialog.setMessage("這是一個圓形進度條對話方塊");                // 設定ProgressDialog 標題表徵圖                m_pDialog.setIcon(R.drawable.img1);                // 設定ProgressDialog 的進度條是否不明確                m_pDialog.setIndeterminate(false);                                // 設定ProgressDialog 是否可以按退回按鍵取消                m_pDialog.setCancelable(true);                                // 設定ProgressDialog 的一個Button                m_pDialog.setButton("確定", new DialogInterface.OnClickListener() {                    public void onClick(DialogInterface dialog, int i)                    {                        //點擊“確定按鈕”取消對話方塊                        dialog.cancel();                    }                });                // 讓ProgressDialog顯示                m_pDialog.show();            }        });              //設定mButton02的事件監聽        mButton02.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v)            {                // TODO Auto-generated method stub                                m_count = 0;                                // 建立ProgressDialog對象                m_pDialog = new ProgressDialog(Activity01.this);                                // 設定進度條風格,風格為長形                m_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);                                // 設定ProgressDialog 標題                m_pDialog.setTitle("提示");                                // 設定ProgressDialog 提示資訊                m_pDialog.setMessage("這是一個長形對話方塊進度條");                                // 設定ProgressDialog 標題表徵圖                m_pDialog.setIcon(R.drawable.img2);                                // 設定ProgressDialog 進度條進度                m_pDialog.setProgress(100);                                // 設定ProgressDialog 的進度條是否不明確                m_pDialog.setIndeterminate(false);                                // 設定ProgressDialog 是否可以按退回按鍵取消                m_pDialog.setCancelable(true);                                // 讓ProgressDialog顯示                m_pDialog.show();                                new Thread()                 {                    public void run()                    {                        try                        {                            while (m_count <= 100)                            {                                 // 由線程來控制進度。                                m_pDialog.setProgress(m_count++);                                Thread.sleep(100);                             }                            m_pDialog.cancel();                        }                        catch (InterruptedException e)                        {                            m_pDialog.cancel();                        }                    }                }.start();                            }        });    }}

 

布局檔案

<?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:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    />  <Button    android:id="@+id/Button01"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="圓形進度條" />  <Button    android:id="@+id/Button02"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="長形進度條" /></LinearLayout>

 

Android-進階UI控制項_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.