• Android Video Tutorials

Android - 載入微調器



您可以在 Android 中透過載入進度條顯示任務的進度。進度條有兩種形狀:載入條和載入微調器。本章將討論微調器。

微調器用於顯示那些完成總時間未知的任務的進度。為了使用它,您只需要在 xml 中這樣定義它。

<ProgressBar
   android:id="@+id/progressBar1"
   style="?android:attr/progressBarStyleLarge"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true" />

在 xml 中定義之後,您必須透過 ProgressBar 類在 java 檔案中獲取其引用。其語法如下:

private ProgressBar spinner;
spinner = (ProgressBar)findViewById(R.id.progressBar1);

之後,您可以使其消失,並在需要時透過 setVisibility 方法將其重新顯示。其語法如下:

spinner.setVisibility(View.GONE);
spinner.setVisibility(View.VISIBLE);	

除了這些方法之外,ProgressBar 類中還定義了其他方法,您可以使用這些方法更有效地處理微調器。

序號 方法和描述
1

isIndeterminate()

指示此進度條是否處於不確定模式

2

postInvalidate()

導致在後續的事件迴圈週期中發生無效操作

3

setIndeterminate(boolean indeterminate)

更改此進度條的不確定模式

4

invalidateDrawable(Drawable dr)

使指定的 Drawable 無效

5

incrementSecondaryProgressBy(int diff)

將進度條的次要進度增加指定數量

6

getProgressDrawable()

獲取用於在進度模式下繪製進度條的可繪製物件

示例

這是一個演示使用 ProgressBar 處理微調器的示例。它建立一個基本的應用程式,允許您單擊按鈕開啟微調器。

要試驗此示例,您可以在實際裝置或模擬器上執行它。

步驟 描述
1 您將使用 Android Studio 在 com.example.sairamkrishna.myapplication 包下建立一個 Android 應用程式。
2 修改 src/MainActivity.java 檔案以新增必要的程式碼。
3 修改 res/layout/activity_main 以新增相應的 XML 元件
4 需要在 drawable 資料夾中建立一個 xml 檔案。它包含有關進度條的形狀和旋轉資訊
5 執行應用程式並選擇正在執行的 Android 裝置,將應用程式安裝在其上並驗證結果

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

package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;


public class MainActivity extends Activity {
   Button b1;

   private ProgressBar spinner;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
        
      b1=(Button)findViewById(R.id.button);
      spinner=(ProgressBar)findViewById(R.id.progressBar);
      spinner.setVisibility(View.GONE);

      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            spinner.setVisibility(View.VISIBLE);
         }
      });
   }
}

以下是修改後的 xml 檔案 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="Progress Dialog" 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_below="@+id/imageView"
      android:layout_centerHorizontal="true" />
      
   <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" />
      
   <ProgressBar
      style="?android:attr/progressBarStyleLarge"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/progressBar"
      android:progressDrawable="@drawable/circular_progress_bar"
      android:layout_below="@+id/button"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"
      android:layout_alignLeft="@+id/textview"
      android:layout_alignStart="@+id/textview"
      android:layout_alignParentBottom="true" />

</RelativeLayout>

以下是 res/drawable/circular_progress_bar.xml 的內容。

<?xml version="1.0" encoding="utf-8"?>
<rotate
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromDegrees="90"
   android:pivotX="50%"
   android:pivotY="50%"
   android:toDegrees="360">
   
   <shape
      android:innerRadiusRatio="3"
      android:shape="ring"
      android:thicknessRatio="7.0">
      
      <gradient
         android:centerColor="#007DD6"
         android:endColor="#007DD6"
         android:startColor="#007DD6"
         android:angle="0"
         android:type="sweep"
         android:useLevel="false" />
   </shape>
   
</rotate>

以下是 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>

讓我們嘗試執行我們剛剛修改的應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟專案的一個活動檔案,然後單擊工具欄中的執行 Eclipse Run Icon 圖示。Android Studio 將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:

Anroid Loading Spinner Tutorial

現在單擊載入微調器按鈕以開啟載入微調器。它顯示在下面的圖片中:

Anroid Loading Spinner Tutorial
廣告