向应用中添加旋转图标
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
微调框提供了一种方法,可让用户从值集内快速选择一个值。默认情况下
状态,旋转图标会显示其当前所选的值。点按旋转图标
会显示一个菜单,其中显示用户可以选择的所有其他值。
图 1. 一个旋转图标菜单,其中显示了
值。
您可以使用
Spinner
对象,您通常在 XML 布局中
<Spinner>
元素。具体可见于
示例:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
要使用选项列表填充旋转图标,请指定
SpinnerAdapter
在您的
Activity
或
Fragment
源代码。
如果您使用 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
个对象
会收到一个 on-item-selected 事件。
要为旋转图标定义选择事件处理脚本,请实现
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):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"]],["最后更新时间 (UTC):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."]]