編輯:Android 開發實例
在 Android 中,可以透過進度條來向使用者顯示某項任務的執行進展。進度條主要分為兩種形態:進度列與旋轉式載入指示器(Spinner)。本章將聚焦於後者。
Spinner 適合用於那些「無法預估總耗時」的任務,用來表示目前有工作正在進行。要使用它,只需在 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 中定義好之後,還需要在 Java 檔裡透過 ProgressBar 類別取得它的參照,語法如下:
private ProgressBar spinner; spinner = (ProgressBar)findViewById(R.id.progressBar1);
之後就能透過 setVisibility 方法讓它隱藏,或在需要時再顯示出來,語法如下:
spinner.setVisibility(View.GONE); spinner.setVisibility(View.VISIBLE);
除了這些方法之外,ProgressBar 類別還定義了許多其他方法,能讓你更有效率地操作 Spinner。
以下範例示範如何以進度條實作 Spinner,打造一個點擊按鈕即可顯示載入指示器的應用。
你可以選擇在實體裝置或模擬器上執行這個範例。
以下是修改後主活動檔案 src/com.yiibai.spinner/MainActivity.java 的內容:
package com.example.spinner; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.ProgressBar; public class MainActivity extends Activity { private ProgressBar spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (ProgressBar)findViewById(R.id.progressBar1); spinner.setVisibility(View.GONE); } public void load(View view){ spinner.setVisibility(View.VISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 以下是修改後 res/layout/activity_main.xml 的內容:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="103dp" android:onClick="load" android:text="@string/hello_world" /> <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" /> </RelativeLayout>
以下是 res/values/string.xml 的內容:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner</string> <string name="action_settings">Settings</string> <string name="hello_world">load spinner</string> </resources>
以下是 AndroidManifest.xml 的內容:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.yiibai.spinner" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.yiibai.spinner.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>
接下來執行這個載入 Spinner 的應用。假設你已建立好 AVD 環境,先安裝並啟動它,若一切設定正確且程式沒有問題,就會看到如下的模擬器視窗:
現在點擊載入按鈕,即可開啟載入指示器,如下圖所示:

Android 登入範例:帳密欄位設計與錯誤嘗試管理
登入畫面存在的目的,是向使用者索取憑證以進入特定應用。這類需求很常見,例如要登入 Facebook、微博等服務。 本章說明如何建立一個登入介面,以及如何處理安全性與錯誤嘗
Android 開發自學筆記(五):用程式碼把版面串成真正的功能
好看的骨架已經有了,接下來要讓它真正動起來。只有外表、沒有內涵的花拳繡腿可不行。 從前面幾篇,我們已經能把版面搭成這個樣子: 相關文章:Android 自學開發第四篇之
Android MediaPlayer:打造自己的音樂播放器
Android 在音訊與影片播放上提供了相當完整的支援,而其中最常被直接使用的一條路徑,就是 MediaPlayer 這個類別。它被封裝在 android.media 套
Android 登入頁面教學:從輸入驗證到錯誤鎖定
只要 App 涉及使用者帳號,就少不了登入畫面。它看似簡單,其實要處理的細節不少:密碼要遮蔽、驗證要正確、錯誤要提示,還得防止有人一直試密碼。本章依序把這些環節做完。 建