向应用中添加旋转图标

微调框提供了一种方法,可让用户从值集内快速选择一个值。默认状态下,旋转图标会显示其当前选定的值。点按旋转图标会显示一个菜单,其中包含用户可以选择的所有其他值。

图 1. 一个旋转图标菜单,显示可用值。

您可以使用 Spinner 对象向布局中添加旋转图标,通常在 XML 布局中使用 <Spinner> 元素执行此操作。具体可见以下示例:

<Spinner
    android:id="@+id/planets_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

如需使用选项列表填充旋转图标,请在 ActivityFragment 源代码中指定 SpinnerAdapter

如果您使用的是 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>

对于这样的数组,您可以在 ActivityFragment 中使用以下代码,以便使用 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);

如果您使用 ActivityFragment 实现 AdapterView.OnItemSelectedListener 接口(如上例所示),则可以将 this 作为接口实例传递。