• Android Video Tutorials

Android - 進度圓圈



建立進度圓圈最簡單的方法是使用名為 ProgressDialog 的類。載入條也可以透過該類建立。條形和圓圈之間唯一的邏輯區別在於,前者用於您知道等待特定任務的總時間,而後者用於您不知道等待時間的情況。

為此,您需要例項化此類的一個物件。其語法為:

ProgressDialog progress = new ProgressDialog(this);

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

progress.setMessage("Downloading Music :) ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
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.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
   Button b1;
   private  ProgressDialog progressBar;
   private int progressBarStatus = 0;
   private Handler progressBarbHandler = new Handler();
   private long fileSize = 0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      b1=(Button)findViewById(R.id.button);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("File downloading ...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();
            progressBarStatus = 0;

            fileSize = 0;
            new Thread(new Runnable() {
               public void run() {
                  while (progressBarStatus < 100) {
                     progressBarStatus = downloadFile();

                     try {
                        Thread.sleep(1000);
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     }

                     progressBarbHandler.post(new Runnable() {
                        public void run() {
                           progressBar.setProgress(progressBarStatus);
                        }
                     });
                  }

                  if (progressBarStatus >= 100) {
                     try {
                        Thread.sleep(2000);
                     } catch (InterruptedException e) {
                        e.printStackTrace();
                     }
                     progressBar.dismiss();
                  }
               }
            }).start();
         }
      });
   }
	
   public int downloadFile() {
      while (fileSize <= 1000000) {
         fileSize++;

         if (fileSize == 100000) {
            return 10;
         }else if (fileSize == 200000) {
            return 20;
         }else if (fileSize == 300000) {
            return 30;
         }else if (fileSize == 400000) {
            return 40;
         }else if (fileSize == 500000) {
            return 50;
         }else if (fileSize == 700000) {
            return 70;
         }else if (fileSize == 800000) {
            return 80;
         }
      }
      return 100;
   }
}

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

在以下程式碼中,abc 表示 tutorialspoint.com 的徽標。
<?xml version="1.0" encoding="utf-8"?>
<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:text="Music Palyer" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="download"
      android:id="@+id/button"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:layout_marginBottom="112dp" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
</RelativeLayout>

res/values/string.xml 修改為以下內容。

<resources>
   <string name="app_name">My Application</string>
</resources>

這是預設的 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="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sairamkrishna.myapplication.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 Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。在啟動應用程式之前,Android Studio 將顯示以下視窗以選擇要在其中執行 Android 應用程式的選項。

Android Progress Dialog Tutorial

只需按下按鈕即可啟動進度對話方塊。按下後,將出現以下螢幕。

Android Progress Dialog Tutorial
廣告

© . All rights reserved.