編輯:Android 開發實例
凡是耗時的活兒,都得給用戶一個交代,否則界面一片死寂,誰都會以為程序卡死了。Android 裡承擔這項工作的就是 ProgressBar,而 SeekBar 與 RatingBar 都由它間接派生,三者共享不少特性,因此放在一篇裡講最合適。
ProgressBar 是界面友好度的重要保障。系統內置了多種視覺風格,通過 style 屬性切換:
這裡有個關鍵區別:只有 Horizontal 風格能真正遞推進度,其餘風格都只是循環播放的卸載動畫,你不會看到「走到 70%」這種效果。要用數值表達進度,必須選 Horizontal,再配合下列屬性:
代碼中動態調整進度有兩組方法。setProgress(int) 直接寫入絕對值;incrementProgressBy(int) 則寫入增量,正數遞增、負數遞減。第二進度對應 setSecondaryProgress(int) 與 incrementSecondaryProgressBy(int)。做「每次前進 5%」這類邏輯時,增量方法往往比絕對值方法更省心。
下面的示例把六種 style 全部鋪開對比,並用按鈕動態控制水平進度條的增減與大進度條的顯隱。
布局代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Small" />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Small"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Large" />
- <ProgressBar
- android:id="@+id/pbLarge"
- style="@android:style/Widget.ProgressBar.Large"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Inverse" />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Small.Inverse" />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Small.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Large.Inverse" />
- <ProgressBar
- style="@android:style/Widget.ProgressBar.Large.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="android:style/Widget.ProgressBar.Horizontal" />
- <ProgressBar
- android:id="@+id/pbHor"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- android:secondaryProgress="60" />
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal" >
- <!-- 設置一個按鈕控制水平進度的遞增 -->
- <Button
- android:id="@+id/btnAdd"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=" + " />
- <!-- 設置一個按鈕控制水平進度的遞減 -->
- <Button
- android:id="@+id/btnReduce"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="30dp"
- android:text=" - " />
- <!-- 設置一個按鈕控制Style為large的進度顯示與隱藏 -->
- <Button
- android:id="@+id/btnVisible"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="30dp"
- android:text="VisibleLarge" />
- </LinearLayout>
- </LinearLayout>
實現代碼:
- package com.bgxt.progressbarseriesdemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ProgressBar;
- public class ProgressBarActivity extends Activity {
- private Button btnAdd, btnReduce, btnVisible;
- private ProgressBar pbHor, pbLarge;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_progressbar);
- btnAdd = (Button) findViewById(R.id.btnAdd);
- btnReduce = (Button) findViewById(R.id.btnReduce);
- btnVisible = (Button) findViewById(R.id.btnVisible);
- pbHor = (ProgressBar) findViewById(R.id.pbHor);
- pbLarge = (ProgressBar) findViewById(R.id.pbLarge);
- btnAdd.setOnClickListener(mathClick);
- btnReduce.setOnClickListener(mathClick);
- btnVisible.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- // 判斷Large進度條是否顯示,顯示則隱藏,隱藏則顯示
- if (pbLarge.getVisibility() == View.VISIBLE) {
- pbLarge.setVisibility(View.GONE);
- } else {
- pbLarge.setVisibility(View.VISIBLE);
- }
- }
- });
- }
- private View.OnClickListener mathClick = new OnClickListener() {
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btnAdd:
- // 如果是增加按鈕,因為進度條的最大值限制在100,第一刻度限制在90.
- // 在此限度內,以1.2倍遞增
- // 使用setProgress()
- if (pbHor.getProgress() < 90) {
- pbHor.setProgress((int) (pbHor.getProgress() * 1.2));
- }
- if (pbHor.getSecondaryProgress() < 100) {
- pbHor.setSecondaryProgress((int) (pbHor
- .getSecondaryProgress() * 1.2));
- }
- break;
- case R.id.btnReduce:
- // 如果是遞減按鈕,進度條最大值100,第一刻度下限10,第二刻度下限20
- // 在此限度內,以10點為基數進行遞減
- // 使用incrementXxxProgressBy(int)
- if (pbHor.getProgress() > 10) {
- pbHor.incrementProgressBy(-10);
- }
- if (pbHor.getSecondaryProgress() > 20) {
- pbHor.incrementSecondaryProgressBy(-10);
- }
- break;
- }
- }
- };
- }
SeekBar 間接繼承自 ProgressBar,因此上面那些屬性它照單全收。差別在表達方式:進度條用顏色填充表示完成度,拖動條則用滑塊位置表示數值。
因為多了「滑塊」這個實體,SeekBar 新增了 android:thumb 屬性,可以傳入一個 Drawable 自定義滑塊外觀;不設它也沒關係,會沿用系統默認樣式。
用戶拖動滑塊時觸發 SeekBar.OnSeekBarChangeListener,這是一個需要實現三個方法的接口:
fromUser 這個參數實戰中很有價值:在初始化代碼裡調用 setProgress() 同樣會觸發 onProgressChanged,若不想讓初始化動作誤觸業務邏輯,就靠它來過濾。
布局代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/textview1"
- android:layout_width="match_parent"
- android:layout_height="30dp" />
- <TextView
- android:id="@+id/textview2"
- android:layout_width="match_parent"
- android:layout_height="30dp" />
- <SeekBar
- android:layout_marginTop="30dp"
- android:id="@+id/seekbar1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="30" />
- <!--設置一個拖動條,滑塊為定義的bar圖片-->
- <SeekBar
- android:layout_marginTop="30dp"
- android:id="@+id/seekbar2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="20"
- android:thumb="@drawable/bar"
- android:secondaryProgress="80" />
- </LinearLayout>
實現代碼:
- package com.bgxt.progressbarseriesdemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.SeekBar;
- import android.widget.TextView;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- public class SeekBarActivity extends Activity {
- private TextView textview1, textview2;
- private SeekBar seekbar1, seekbar2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_seekbar);
- textview1 = (TextView) findViewById(R.id.textview1);
- textview2 = (TextView) findViewById(R.id.textview2);
- seekbar1 = (SeekBar) findViewById(R.id.seekbar1);
- seekbar2 = (SeekBar) findViewById(R.id.seekbar2);
- seekbar1.setOnSeekBarChangeListener(seekBarChange);
- seekbar2.setOnSeekBarChangeListener(seekBarChange);
- }
- private OnSeekBarChangeListener seekBarChange = new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- if (seekBar.getId() == R.id.seekbar1) {
- textview1.setText("seekbar1停止拖動");
- } else {
- textview1.setText("seekbar2停止拖動");
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- if (seekBar.getId() == R.id.seekbar1) {
- textview1.setText("seekbar1開始拖動");
- } else {
- textview1.setText("seekbar2開始拖動");
- }
- }
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- if (seekBar.getId() == R.id.seekbar1) {
- textview2.setText("seekbar1的當前位置是:" + progress);
- } else {
- textview2.setText("seekbar2的當前位置是:" + progress);
- }
- }
- };
- }
RatingBar 與 SeekBar 擁有共同的父類 AbsSeekBar,而 AbsSeekBar 又繼承自 ProgressBar。兩者用法極為接近,最直觀的區別是:RatingBar 用星形圖標而非色塊來表達進度。
它擴充了一批專屬屬性,同時屏蔽了第二進度這種用不上的概念:
星級變化時觸發 RatingBar.OnRatingBarChangeListener,需實現:
onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
這裡有個概念必須分清:RatingBar 同時存在 progress 與 rating 兩個值。progress 是相對 max 而言的刻度值,rating 才是「第幾顆星」。示例中把 max 設為 100、進度設為 20,此時 rating 未必等於 20,兩者不可混用。
布局代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="評分控件的使用"
- android:textSize="20dp" />
- <RatingBar
- android:id="@+id/rbRating"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <RatingBar
- android:id="@+id/rbRating1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:isIndicator="false"
- android:max="100"
- android:numStars="4"
- android:rating="2.5"
- android:stepSize="0.5" />
- </LinearLayout>
實現代碼:
- package com.bgxt.progressbarseriesdemo;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.RatingBar;
- import android.widget.RatingBar.OnRatingBarChangeListener;
- import android.widget.Toast;
- public class RatingBarActivity extends Activity implements OnRatingBarChangeListener {
- private RatingBar rbRating, rbRating1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_ratingbar);
- rbRating=(RatingBar)findViewById(R.id.rbRating);
- rbRating1=(RatingBar)findViewById(R.id.rbRating1);
- //手動設置第一個RatingBar的屬性值
- rbRating.setMax(100);
- rbRating.setProgress(20);
- rbRating.setOnRatingBarChangeListener(this);
- rbRating1.setOnRatingBarChangeListener(this);
- }
- @Override
- public void onRatingChanged(RatingBar ratingBar, float rating,
- boolean fromUser) {
- //分別顯示Progress屬性和rating屬性的不同
- int progress=ratingBar.getProgress();
- Toast.makeText(RatingBarActivity.this, "progress:"+progress+" rating :"+rating,Toast.LENGTH_SHORT).show();
- }
- }
三個控件一脈相承:ProgressBar 定地基,SeekBar 加入人機交互,RatingBar 換上星形外衣。本文只談用法,自定義樣式與 Drawable 更換留待後續。理解繼承鏈的好處是,學會一個之後,另兩個的屬性與監聽幾乎可以類推。
Android 進階:藍牙隱藏 API 探秘
前文講解了 Android 藍牙的基本用法,本文則更深入一步,探討藍牙方面的隱藏 API。用過 Android 系統設置的人都知道,藍牙搜索之後可以建立配對或解除配對,但
Android 指南針實作:用方向感測器打造指北 App
想製作一個指北針 App 嗎?Android 的方向感測器(Orientation Sensor)能即時回報裝置朝向,搭配一張指北針圖片與旋轉動畫,就能輕鬆實現。以下分四
以百度地圖 SDK 實現 Android 定位功能:完整流程解析
LBS 位置服務是 Android 應用中愈來愈常見的能力,從定位、地圖顯示到導航,都建立在它之上。本文以百度地圖定位 SDK 為例,逐步示範如何完成一次定位並把結果回傳
Android 編程:監聽 EditText 內容變化的方法
本文介紹如何監聽 EditText 中內容的變化,供大家參考。 我們常常需要實時監聽輸入框裡文字的變化,比如根據輸入內容動態顯示或隱藏某個按鈕。翻了很久發現 EditTe