Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 應用開發入門(二十一):ProgressBar、SeekBar 與 RatingBar 全解

編輯:Android 開發實例

導讀

凡是耗時的活兒,都得給用戶一個交代,否則界面一片死寂,誰都會以為程序卡死了。Android 裡承擔這項工作的就是 ProgressBar,而 SeekBar 與 RatingBar 都由它間接派生,三者共享不少特性,因此放在一篇裡講最合適。

  • ProgressBar:進度條,向用戶呈現耗時任務的完成百分比。
  • SeekBar:拖動條,用戶拖動滑塊來改變數值。
  • RatingBar:星級評分條,用戶點選星級完成打分。

ProgressBar

ProgressBar 是界面友好度的重要保障。系統內置了多種視覺風格,通過 style 屬性切換:

  • @android:style/Widget.ProgressBar.Horizontal:水平進度條,可顯示刻度,實戰中最常用。
  • @android:style/Widget.ProgressBar.Small:小尺寸轉圈。
  • @android:style/Widget.ProgressBar.Large:大尺寸轉圈。
  • @android:style/Widget.ProgressBar.Inverse:帶跳躍旋轉動畫的進度條。
  • @android:style/Widget.ProgressBar.Large.Inverse / Small.Inverse:動畫版的大、小進度條。

這裡有個關鍵區別:只有 Horizontal 風格能真正遞推進度,其餘風格都只是循環播放的卸載動畫,你不會看到「走到 70%」這種效果。要用數值表達進度,必須選 Horizontal,再配合下列屬性:

  • android:max:進度上限。
  • android:progress:第一進度值。
  • android:secondaryProgress:第二進度值(常用於緩存或預加載表達)。
  • android:visibility:是否顯示,默認顯示。

代碼中動態調整進度有兩組方法。setProgress(int) 直接寫入絕對值;incrementProgressBy(int) 則寫入增量,正數遞增、負數遞減。第二進度對應 setSecondaryProgress(int) 與 incrementSecondaryProgressBy(int)。做「每次前進 5%」這類邏輯時,增量方法往往比絕對值方法更省心。

下面的示例把六種 style 全部鋪開對比,並用按鈕動態控制水平進度條的增減與大進度條的顯隱。

布局代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="android:style/Widget.ProgressBar.Small" /> 
  11.  
  12.     <ProgressBar 
  13.         style="@android:style/Widget.ProgressBar.Small" 
  14.         android:layout_width="wrap_content" 
  15.         android:layout_height="wrap_content" /> 
  16.  
  17.     <TextView 
  18.         android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" 
  20.         android:text="android:style/Widget.ProgressBar.Large" /> 
  21.  
  22.     <ProgressBar 
  23.         android:id="@+id/pbLarge" 
  24.         style="@android:style/Widget.ProgressBar.Large" 
  25.         android:layout_width="wrap_content" 
  26.         android:layout_height="wrap_content" /> 
  27.  
  28.     <TextView 
  29.         android:layout_width="wrap_content" 
  30.         android:layout_height="wrap_content" 
  31.         android:text="android:style/Widget.ProgressBar.Inverse" /> 
  32.  
  33.     <ProgressBar 
  34.         style="@android:style/Widget.ProgressBar.Inverse" 
  35.         android:layout_width="wrap_content" 
  36.         android:layout_height="wrap_content" /> 
  37.  
  38.     <TextView 
  39.         android:layout_width="wrap_content" 
  40.         android:layout_height="wrap_content" 
  41.         android:text="android:style/Widget.ProgressBar.Small.Inverse" /> 
  42.  
  43.     <ProgressBar 
  44.         style="@android:style/Widget.ProgressBar.Small.Inverse" 
  45.         android:layout_width="wrap_content" 
  46.         android:layout_height="wrap_content" /> 
  47.  
  48.     <TextView 
  49.         android:layout_width="wrap_content" 
  50.         android:layout_height="wrap_content" 
  51.         android:text="android:style/Widget.ProgressBar.Large.Inverse" /> 
  52.  
  53.     <ProgressBar 
  54.         style="@android:style/Widget.ProgressBar.Large.Inverse" 
  55.         android:layout_width="wrap_content" 
  56.         android:layout_height="wrap_content" /> 
  57.  
  58.     <TextView 
  59.         android:layout_width="wrap_content" 
  60.         android:layout_height="wrap_content" 
  61.         android:text="android:style/Widget.ProgressBar.Horizontal" /> 
  62.  
  63.     <ProgressBar 
  64.         android:id="@+id/pbHor" 
  65.         style="@android:style/Widget.ProgressBar.Horizontal" 
  66.         android:layout_width="match_parent" 
  67.         android:layout_height="wrap_content" 
  68.         android:max="100" 
  69.         android:progress="20" 
  70.         android:secondaryProgress="60" /> 
  71.  
  72.     <LinearLayout 
  73.         android:layout_width="match_parent" 
  74.         android:layout_height="match_parent" 
  75.         android:orientation="horizontal" > 
  76.         <!-- 設置一個按鈕控制水平進度的遞增 --> 
  77.         <Button 
  78.             android:id="@+id/btnAdd" 
  79.             android:layout_width="wrap_content" 
  80.             android:layout_height="wrap_content" 
  81.             android:text="  +  " /> 
  82.         <!-- 設置一個按鈕控制水平進度的遞減 --> 
  83.         <Button 
  84.             android:id="@+id/btnReduce" 
  85.             android:layout_width="wrap_content" 
  86.             android:layout_height="wrap_content" 
  87.             android:layout_marginLeft="30dp" 
  88.             android:text="  -  " /> 
  89.         <!-- 設置一個按鈕控制Style為large的進度顯示與隱藏 --> 
  90.         <Button 
  91.             android:id="@+id/btnVisible" 
  92.             android:layout_width="wrap_content" 
  93.             android:layout_height="wrap_content" 
  94.             android:layout_marginLeft="30dp" 
  95.             android:text="VisibleLarge" /> 
  96.     </LinearLayout> 
  97.  
  98. </LinearLayout> 

實現代碼:

  1. package com.bgxt.progressbarseriesdemo;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.ProgressBar;  
  9.  
  10. public class ProgressBarActivity extends Activity {  
  11.  
  12.     private Button btnAdd, btnReduce, btnVisible;  
  13.     private ProgressBar pbHor, pbLarge;  
  14.  
  15.     @Override 
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_progressbar);  
  19.  
  20.         btnAdd = (Button) findViewById(R.id.btnAdd);  
  21.         btnReduce = (Button) findViewById(R.id.btnReduce);  
  22.         btnVisible = (Button) findViewById(R.id.btnVisible);  
  23.         pbHor = (ProgressBar) findViewById(R.id.pbHor);  
  24.         pbLarge = (ProgressBar) findViewById(R.id.pbLarge);  
  25.  
  26.         btnAdd.setOnClickListener(mathClick);  
  27.         btnReduce.setOnClickListener(mathClick);  
  28.         btnVisible.setOnClickListener(new View.OnClickListener() {  
  29.  
  30.             @Override 
  31.             public void onClick(View v) {  
  32.                 // 判斷Large進度條是否顯示,顯示則隱藏,隱藏則顯示  
  33.                 if (pbLarge.getVisibility() == View.VISIBLE) {  
  34.                     pbLarge.setVisibility(View.GONE);  
  35.                   } else {  
  36.                     pbLarge.setVisibility(View.VISIBLE);  
  37.                }  
  38.  
  39.             }  
  40.         });  
  41.     }  
  42.  
  43.     private View.OnClickListener mathClick = new OnClickListener() {  
  44.  
  45.         @Override 
  46.         public void onClick(View v) {  
  47.             switch (v.getId()) {  
  48.                 case R.id.btnAdd: 
  49.                     // 如果是增加按鈕,因為進度條的最大值限制在100,第一刻度限制在90. 
  50.                     // 在此限度內,以1.2倍遞增 
  51.                     // 使用setProgress() 
  52.                     if (pbHor.getProgress() < 90) {  
  53.                       pbHor.setProgress((int) (pbHor.getProgress() * 1.2));  
  54.                  }  
  55.                  if (pbHor.getSecondaryProgress() < 100) {  
  56.                     pbHor.setSecondaryProgress((int) (pbHor 
  57.                             .getSecondaryProgress() * 1.2));  
  58.                 }  
  59.                 break;  
  60.                 case R.id.btnReduce: 
  61.                     // 如果是遞減按鈕,進度條最大值100,第一刻度下限10,第二刻度下限20 
  62.                     // 在此限度內,以10點為基數進行遞減 
  63.                     // 使用incrementXxxProgressBy(int) 
  64.                     if (pbHor.getProgress() > 10) {  
  65.                       pbHor.incrementProgressBy(-10);  
  66.                     }  
  67.                     if (pbHor.getSecondaryProgress() > 20) {  
  68.                        pbHor.incrementSecondaryProgressBy(-10);  
  69.                  }  
  70.                 break;  
  71.  
  72.             }  
  73.         }  
  74.     };
  75.  
  76.  
  77. } 

SeekBar

SeekBar 間接繼承自 ProgressBar,因此上面那些屬性它照單全收。差別在表達方式:進度條用顏色填充表示完成度,拖動條則用滑塊位置表示數值。

因為多了「滑塊」這個實體,SeekBar 新增了 android:thumb 屬性,可以傳入一個 Drawable 自定義滑塊外觀;不設它也沒關係,會沿用系統默認樣式。

用戶拖動滑塊時觸發 SeekBar.OnSeekBarChangeListener,這是一個需要實現三個方法的接口:

  • onProgressChanged(SeekBar seekBar, int progress, boolean fromUser):滑塊移動中持續回調。progress 為當前值,fromUser 用於區分在用戶拖動還是代碼賦值。
  • onStartTrackingTouch(SeekBar seekBar):開始拖動時回調。
  • onStopTrackingTouch(SeekBar seekBar):結束拖動時回調。

fromUser 這個參數實戰中很有價值:在初始化代碼裡調用 setProgress() 同樣會觸發 onProgressChanged,若不想讓初始化動作誤觸業務邏輯,就靠它來過濾。

布局代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.         <TextView 
  7.             
  8.         android:id="@+id/textview1" 
  9.         android:layout_width="match_parent" 
  10.         android:layout_height="30dp" /> 
  11.  
  12.     <TextView 
  13.         android:id="@+id/textview2" 
  14.         android:layout_width="match_parent" 
  15.         android:layout_height="30dp" /> 
  16.  
  17.     <SeekBar 
  18.         android:layout_marginTop="30dp" 
  19.         android:id="@+id/seekbar1" 
  20.         android:layout_width="match_parent" 
  21.         android:layout_height="wrap_content" 
  22.         android:max="100" 
  23.         android:progress="30" /> 
  24.     <!--設置一個拖動條,滑塊為定義的bar圖片--> 
  25.     <SeekBar 
  26.         android:layout_marginTop="30dp" 
  27.         android:id="@+id/seekbar2" 
  28.         android:layout_width="match_parent" 
  29.         android:layout_height="wrap_content" 
  30.         android:max="100" 
  31.         android:progress="20" 
  32.         android:thumb="@drawable/bar" 
  33.         android:secondaryProgress="80" /> 
  34.  
  35. </LinearLayout> 

實現代碼:

  1. package com.bgxt.progressbarseriesdemo;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.SeekBar;  
  6. import android.widget.TextView;  
  7. import android.widget.SeekBar.OnSeekBarChangeListener;  
  8.  
  9. public class SeekBarActivity extends Activity {  
  10.     private TextView textview1, textview2;  
  11.     private SeekBar seekbar1, seekbar2;  
  12.     @Override 
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         // TODO Auto-generated method stub 
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_seekbar);  
  17.         
  18.         textview1 = (TextView) findViewById(R.id.textview1);  
  19.         textview2 = (TextView) findViewById(R.id.textview2);  
  20.         seekbar1 = (SeekBar) findViewById(R.id.seekbar1);  
  21.         seekbar2 = (SeekBar) findViewById(R.id.seekbar2);  
  22.  
  23.         seekbar1.setOnSeekBarChangeListener(seekBarChange);  
  24.         seekbar2.setOnSeekBarChangeListener(seekBarChange);  
  25.     }  
  26.     private OnSeekBarChangeListener seekBarChange = new OnSeekBarChangeListener() {  
  27.  
  28.         @Override 
  29.         public void onStopTrackingTouch(SeekBar seekBar) {  
  30.             if (seekBar.getId() == R.id.seekbar1) {  
  31.                 textview1.setText("seekbar1停止拖動");  
  32.             } else {  
  33.                 textview1.setText("seekbar2停止拖動");  
  34.         }  
  35.        }  
  36.  
  37.         @Override 
  38.         public void onStartTrackingTouch(SeekBar seekBar) {  
  39.             if (seekBar.getId() == R.id.seekbar1) {  
  40.                 textview1.setText("seekbar1開始拖動");  
  41.             } else {  
  42.                 textview1.setText("seekbar2開始拖動");  
  43.         }  
  44.        }  
  45.  
  46.         @Override 
  47.         public void onProgressChanged(SeekBar seekBar, int progress, 
  48.                 boolean fromUser) {  
  49.             if (seekBar.getId() == R.id.seekbar1) {  
  50.                 textview2.setText("seekbar1的當前位置是:" + progress);  
  51.             } else {  
  52.                 textview2.setText("seekbar2的當前位置是:" + progress);  
  53.            }  
  54.  
  55.        }  
  56.     };
  57. } 

RatingBar

RatingBar 與 SeekBar 擁有共同的父類 AbsSeekBar,而 AbsSeekBar 又繼承自 ProgressBar。兩者用法極為接近,最直觀的區別是:RatingBar 用星形圖標而非色塊來表達進度。

它擴充了一批專屬屬性,同時屏蔽了第二進度這種用不上的概念:

  • android:isIndicator:是否只讀。true 表示用戶不可修改,默認 false。
  • android:numStars:展示幾顆星,默認 5。
  • android:rating:初始星級數。
  • android:stepSize:每次變動的星級步長,支持半星。

星級變化時觸發 RatingBar.OnRatingBarChangeListener,需實現:

onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)

這裡有個概念必須分清:RatingBar 同時存在 progress 與 rating 兩個值。progress 是相對 max 而言的刻度值,rating 才是「第幾顆星」。示例中把 max 設為 100、進度設為 20,此時 rating 未必等於 20,兩者不可混用。

布局代碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:layout_width="match_parent" 
  4.     android:layout_height="match_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <TextView 
  8.         android:layout_width="match_parent" 
  9.         android:layout_height="wrap_content" 
  10.         android:text="評分控件的使用" 
  11.         android:textSize="20dp" /> 
  12.  
  13.     <RatingBar 
  14.         android:id="@+id/rbRating" 
  15.         android:layout_width="wrap_content" 
  16.         android:layout_height="wrap_content" /> 
  17.  
  18.     <RatingBar 
  19.         android:id="@+id/rbRating1" 
  20.         android:layout_width="wrap_content" 
  21.         android:layout_height="wrap_content" 
  22.         android:isIndicator="false" 
  23.         android:max="100" 
  24.         android:numStars="4" 
  25.         android:rating="2.5" 
  26.         android:stepSize="0.5" /> 
  27.  
  28. </LinearLayout> 

實現代碼:

  1. package com.bgxt.progressbarseriesdemo;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RatingBar;  
  6. import android.widget.RatingBar.OnRatingBarChangeListener;  
  7. import android.widget.Toast;  
  8.  
  9. public class RatingBarActivity extends Activity implements OnRatingBarChangeListener {  
  10.  
  11.     private RatingBar rbRating, rbRating1;  
  12.     @Override 
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_ratingbar);  
  16.         rbRating=(RatingBar)findViewById(R.id.rbRating);  
  17.         rbRating1=(RatingBar)findViewById(R.id.rbRating1);  
  18.         //手動設置第一個RatingBar的屬性值 
  19.         rbRating.setMax(100);  
  20.         rbRating.setProgress(20);  
  21.         rbRating.setOnRatingBarChangeListener(this);  
  22.         rbRating1.setOnRatingBarChangeListener(this);  
  23.     }  
  24.     @Override 
  25.     public void onRatingChanged(RatingBar ratingBar, float rating, 
  26.             boolean fromUser) {  
  27.         //分別顯示Progress屬性和rating屬性的不同 
  28.         int progress=ratingBar.getProgress();  
  29.         Toast.makeText(RatingBarActivity.this, "progress:"+progress+" rating :"+rating,Toast.LENGTH_SHORT).show();  
  30.     }  
  31.  
  32. } 

小結

三個控件一脈相承:ProgressBar 定地基,SeekBar 加入人機交互,RatingBar 換上星形外衣。本文只談用法,自定義樣式與 Drawable 更換留待後續。理解繼承鏈的好處是,學會一個之後,另兩個的屬性與監聽幾乎可以類推。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved