編輯: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 JSON 解析教學:用 JSONObject 讀懂天氣 API 回傳的資料
JSON(JavaScript Object Notation,JavaScript 物件符號法)是一種以純文字為基礎、與程式語言無關的資料交換格式。因為結構輕量、可讀性
descendantFocusability 用法解析:解決 ListView 項目點擊沒反應的問題
適用情境 這個屬性適用於「母容器無法取得焦點」的所有情況,最典型的就是自定義 ListView 項目。 問題現場 專案裡的 ListView 很少只是純文字,通常要自定
Android 選單機制(二):上下文選單與 ActionMode
上一篇談完了 Options Menu,這次接著看另外兩種選單形式:上下文選單(Context Menu)與上下文操作模式(Contextual Action Mode)
Android Virtual Device 建立與啟動教學
AVD 是什麼 Android Virtual Device 是 Android 模擬器的虛擬裝置設定。每個 AVD 可指定系統版本、螢幕配置與硬體參數,讓開發者模擬不同