• Android Video Tutorials

使用ProgressDialog的Android進度條



進度條用於顯示任務的進度。例如,當您從網際網路上傳下載某些內容時,最好向使用者顯示下載/上傳進度。

在Android中,有一個名為ProgressDialog的類允許您建立進度條。為此,您需要例項化此類的物件。其語法如下。

ProgressDialog progress = new ProgressDialog(this);

現在您可以設定此對話方塊的一些屬性。例如,它的樣式、文字等。

progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(true);

除了這些方法之外,ProgressDialog類還提供了其他方法。

序號 標題和描述
1

getMax()

此方法返回進度的最大值。

2

incrementProgressBy(int diff)

此方法將進度條增加傳遞的引數值差。

3

setIndeterminate(boolean indeterminate)

此方法將進度指示器設定為確定性或不確定性。

4

setMax(int max)

此方法設定進度對話方塊的最大值。

5

setProgress(int value)

此方法用於使用特定值更新進度對話方塊。

6

show(Context context, CharSequence title, CharSequence message)

這是一個靜態方法,用於顯示進度對話方塊。

示例

此示例演示了進度對話方塊(實際上是進度條)的水平使用。它在按下按鈕時顯示進度條。

要試驗此示例,您需要在按照以下步驟開發應用程式後,在一個實際裝置上執行它。

步驟 描述
1 您將使用Android Studio在com.example.sairamkrishna.myapplication包下建立一個Android應用程式。
2 修改src/MainActivity.java檔案以新增顯示進度對話方塊的進度程式碼。
3 修改res/layout/activity_main.xml檔案以新增相應的XML程式碼。
4 執行應用程式,選擇正在執行的Android裝置,並在其上安裝應用程式,然後驗證結果。

以下是修改後的主活動檔案src/MainActivity.java的內容。

package com.example.sairamkrishna.myapplication;

import android.app.ProgressDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {
   Button b1;
   private ProgressDialog progress;
   
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1 = (Button) findViewById(R.id.button2);
   }
   
   public void download(View view){
      progress=new ProgressDialog(this);
      progress.setMessage("Downloading Music");
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.setProgress(0);
      progress.show();
      
      final int totalProgressTime = 100;
      final Thread t = new Thread() {
         @Override
         public void run() {
            int jumpTime = 0;
            
            while(jumpTime < totalProgressTime) {
               try {
                  sleep(200);
                  jumpTime += 5;
                  progress.setProgress(jumpTime);
               } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
               }
            }
         }
      };
      t.start();
   }
}

res/layout/activity_main.xml的內容修改為以下內容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
   android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
   
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp"
      android:text="Progress bar" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials Point"
      android:id="@+id/textView2"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:textSize="35dp"
      android:textColor="#ff16ff01" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Download"
      android:onClick="download"
      android:id="@+id/button2"
      android:layout_marginLeft="125dp"
      android:layout_marginStart="125dp"
      android:layout_centerVertical="true" />
      
</RelativeLayout>

這是預設的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
   
      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
   
   </application>
</manifest>

讓我們嘗試執行您的應用程式。我們假設您已將實際的Android移動裝置連線到計算機。要從Android Studio執行應用程式,請開啟專案的其中一個活動檔案,然後單擊工具欄中的執行Eclipse Run Icon圖示。在啟動應用程式之前,Android Studio將顯示以下視窗,供您選擇要在其中執行Android應用程式的選項。

Anroid Camera Tutorial

選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示以下螢幕:

Android Progress Dialog Tutorial

只需按下按鈕即可啟動進度條。按下後,將出現以下螢幕:

Android Progress Dialog Tutorial

它將不斷更新自身。

廣告
© . All rights reserved.