在應用程式中新增旋轉圖示
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
輪轉選單可讓使用者從一組值中輕鬆選取某個值。採用預設值
狀態時,輪轉選單會顯示目前選取的值。輕觸旋轉圖示
會顯示選單,顯示使用者可選取的所有其他值。
圖 1. 旋轉圖示中的選單,顯示可用的選單
輕鬆分配獎金
您可以使用
Spinner
這類物件通常是在 XML 版面配置中使用
<Spinner>
元素。如下所示
範例:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
如要在輪轉選單填入選項清單,請指定
SpinnerAdapter
在您的
Activity
或
Fragment
Cloud Build 觸發條件
會在您變更原始碼時自動啟動建構作業
如果您使用的是 Material Design 元件,
實驗組
下拉式選單等同於 Spinner
。
透過使用者選項填入輪轉選單
您為輪轉選單提供的選項可能來自任何來源,但您可以
必須透過 SpinnerAdapter
提供,例如
ArrayAdapter
如果選項有陣列或陣列中
CursorAdapter
當資料庫查詢有可用的選項時
舉例來說,假設輪轉選單可用的選項已預先決定
您可以提供
字串資源
檔案:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
使用這類陣列時,您可以在
Activity
或 Fragment
,用於提供輪轉選單
使用 ArrayAdapter
例項的陣列:
Kotlin
val spinner: Spinner = findViewById(R.id.planets_spinner)
// Create an ArrayAdapter using the string array and a default spinner layout.
ArrayAdapter.createFromResource(
this,
R.array.planets_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner.
spinner.adapter = adapter
}
Java
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,
R.array.planets_array,
android.R.layout.simple_spinner_item
);
// Specify the layout to use when the list of choices appears.
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner.
spinner.setAdapter(adapter);
createFromResource()
方法可讓您透過字串陣列建立 ArrayAdapter
。
這個方法的第三個引數是版面配置資源,可定義
所選的選項會顯示在輪轉選單控制項中這個平台提供
simple_spinner_item
版面配置。除非您想自行定義版面配置,否則此為預設版面配置
旋轉圖示的呈現方式
致電
setDropDownViewResource(int)
指定轉接程式用於顯示輪轉選單選項清單的版面配置。
simple_spinner_dropdown_item
是由平台定義的另一個標準版面配置
致電
setAdapter()
即可在 Spinner
上套用轉接器。
回覆使用者所選項目
當使用者從輪轉選單的選單中選取項目時,
Spinner
個物件
收到項目選取事件時。
如要定義輪轉選單的選取事件處理常式,請實作
AdapterView.OnItemSelectedListener
敬上
和對應的
onItemSelected()
回呼方法。例如,以下將
Activity
:
Kotlin
class SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {
...
override fun onItemSelected(parent: AdapterView<*>, view: View?, pos: Int, id: Long) {
// An item is selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos).
}
override fun onNothingSelected(parent: AdapterView<*>) {
// Another interface callback.
}
}
Java
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item is selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos).
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback.
}
}
AdapterView.OnItemSelectedListener
介面需要
「onItemSelected()
」和
onNothingSelected()
回呼方法。
呼叫 以指定介面實作
setOnItemSelectedListener()
:
Kotlin
val spinner: Spinner = findViewById(R.id.planets_spinner)
spinner.onItemSelectedListener = this
Java
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
spinner.setOnItemSelectedListener(this);
如果實作 AdapterView.OnItemSelectedListener
與 Activity
或 Fragment
互動,如
上述的例子,您可以傳遞 this
做為介面執行個體。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Add spinners to your app\n\nSpinners provide a quick way to select one value from a set. In the default\nstate, a spinner shows its currently selected value. Tapping the spinner\ndisplays a menu showing all other values the user can select. \n**Figure 1.** A menu from a spinner, showing the available values.\n\nYou can add a spinner to your layout with the\n[Spinner](/reference/android/widget/Spinner)\nobject, which you usually do in your XML layout with a\n`\u003cSpinner\u003e` element. This is shown in the following\nexample: \n\n```xml\n\u003cSpinner\n android:id=\"@+id/planets_spinner\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\" /\u003e\n```\n\nTo populate the spinner with a list of choices, specify a\n[SpinnerAdapter](/reference/android/widget/SpinnerAdapter)\nin your\n[Activity](/reference/android/app/Activity) or\n[Fragment](/reference/androidx/fragment/app/Fragment)\nsource code.\n\nIf you are using Material Design Components,\n[exposed\ndropdown menus](https://www.material.io/components/menus/android#exposed-dropdown-menus) are the equivalent of a `Spinner`.\n\nPopulate the spinner with user choices\n--------------------------------------\n\nThe choices you provide for the spinner can come from any source, but you\nmust provide them through a `SpinnerAdapter`, such as an\n[ArrayAdapter](/reference/android/widget/ArrayAdapter)\nif the choices are available in an array or a\n[CursorAdapter](/reference/android/widget/CursorAdapter)\nif the choices are available from a database query.\n\nFor example, if the available choices for your spinner are pre-determined,\nyou can provide them with a string array defined in a\n[string resource\nfile](/guide/topics/resources/string-resource): \n\n```xml\n\u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n\u003cresources\u003e\n \u003cstring-array name=\"planets_array\"\u003e\n \u003citem\u003eMercury\u003c/item\u003e\n \u003citem\u003eVenus\u003c/item\u003e\n \u003citem\u003eEarth\u003c/item\u003e\n \u003citem\u003eMars\u003c/item\u003e\n \u003citem\u003eJupiter\u003c/item\u003e\n \u003citem\u003eSaturn\u003c/item\u003e\n \u003citem\u003eUranus\u003c/item\u003e\n \u003citem\u003eNeptune\u003c/item\u003e\n \u003c/string-array\u003e\n\u003c/resources\u003e\n```\n\nWith an array like this, you can use the following code in your\n`Activity` or `Fragment` to supply the spinner with the\narray using an instance of `ArrayAdapter`: \n\n### Kotlin\n\n```kotlin\nval spinner: Spinner = findViewById(R.id.planets_spinner)\n// Create an ArrayAdapter using the string array and a default spinner layout.\nArrayAdapter.createFromResource(\n this,\n R.array.planets_array,\n android.R.layout.simple_spinner_item\n).also { adapter -\u003e\n // Specify the layout to use when the list of choices appears.\n adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)\n // Apply the adapter to the spinner.\n spinner.adapter = adapter\n}\n```\n\n### Java\n\n```java\nSpinner spinner = (Spinner) findViewById(R.id.planets_spinner);\n// Create an ArrayAdapter using the string array and a default spinner layout.\nArrayAdapter\u003cCharSequence\u003e adapter = ArrayAdapter.createFromResource(\n this,\n R.array.planets_array,\n android.R.layout.simple_spinner_item\n);\n// Specify the layout to use when the list of choices appears.\nadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);\n// Apply the adapter to the spinner.\nspinner.setAdapter(adapter);\n```\n\nThe\n[createFromResource()](/reference/android/widget/ArrayAdapter#createFromResource(android.content.Context, int, int))\nmethod lets you create an `ArrayAdapter` from the string array. The\nthird argument for this method is a layout resource that defines how the\nselected choice appears in the spinner control. The platform provides the\n[simple_spinner_item](/reference/android/R.layout#simple_spinner_item)\nlayout. This is the default layout unless you want to define your own layout for\nthe spinner's appearance.\n\nCall\n[setDropDownViewResource(int)](/reference/android/widget/ArrayAdapter#setDropDownViewResource(int))\nto specify the layout the adapter uses to display the list of spinner choices.\n[simple_spinner_dropdown_item](/reference/android/R.layout#simple_spinner_dropdown_item)\nis another standard layout defined by the platform.\n\nCall\n[setAdapter()](/reference/android/widget/AdapterView#setAdapter(T))\nto apply the adapter to your `Spinner`.\n\nRespond to user selections\n--------------------------\n\nWhen the user selects an item from the spinner's menu, the\n[Spinner](/reference/android/widget/Spinner) object\nreceives an on-item-selected event.\n\nTo define the selection event handler for a spinner, implement the\n[`AdapterView.OnItemSelectedListener`](/reference/android/widget/AdapterView.OnItemSelectedListener)\ninterface and the corresponding\n[onItemSelected()](/reference/android/widget/AdapterView.OnItemSelectedListener#onItemSelected(android.widget.AdapterView\u003c?\u003e, android.view.View, int, long))\ncallback method. For example, here's an implementation of the interface in an\n`Activity`: \n\n### Kotlin\n\n```kotlin\nclass SpinnerActivity : Activity(), AdapterView.OnItemSelectedListener {\n ...\n override fun onItemSelected(parent: AdapterView\u003c*\u003e, view: View?, pos: Int, id: Long) {\n // An item is selected. You can retrieve the selected item using\n // parent.getItemAtPosition(pos).\n }\n\n override fun onNothingSelected(parent: AdapterView\u003c*\u003e) {\n // Another interface callback.\n }\n}\n```\n\n### Java\n\n```java\npublic class SpinnerActivity extends Activity implements OnItemSelectedListener {\n ...\n public void onItemSelected(AdapterView\u003c?\u003e parent, View view,\n int pos, long id) {\n // An item is selected. You can retrieve the selected item using\n // parent.getItemAtPosition(pos).\n }\n\n public void onNothingSelected(AdapterView\u003c?\u003e parent) {\n // Another interface callback.\n }\n}\n```\n\nThe\n`AdapterView.OnItemSelectedListener` interface requires the\n`onItemSelected()` and\n[onNothingSelected()](/reference/android/widget/AdapterView.OnItemSelectedListener#onNothingSelected(android.widget.AdapterView\u003c?\u003e))\ncallback methods.\n\nSpecify the interface implementation by calling\n[setOnItemSelectedListener()](/reference/android/widget/AdapterView#setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener)): \n\n### Kotlin\n\n```kotlin\nval spinner: Spinner = findViewById(R.id.planets_spinner)\nspinner.onItemSelectedListener = this\n```\n\n### Java\n\n```java\nSpinner spinner = (Spinner) findViewById(R.id.planets_spinner);\nspinner.setOnItemSelectedListener(this);\n```\n\nIf you implement the `AdapterView.OnItemSelectedListener`\ninterface with your `Activity` or `Fragment`, as in the\npreceding example, you can pass `this` as the interface instance."]]