在應用程式中新增旋轉圖示

輪轉選單可讓使用者從一組值中輕鬆選取某個值。採用預設值 狀態時,輪轉選單會顯示目前選取的值。輕觸旋轉圖示 會顯示選單,顯示使用者可選取的所有其他值。

圖 1. 旋轉圖示中的選單,顯示可用的選單 輕鬆分配獎金

您可以使用 Spinner 這類物件通常是在 XML 版面配置中使用 <Spinner> 元素。如下所示 範例:

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

如要在輪轉選單填入選項清單,請指定 SpinnerAdapter 在您的 ActivityFragment 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>

使用這類陣列時,您可以在 ActivityFragment,用於提供輪轉選單 使用 ArrayAdapter 例項的陣列:

KotlinJava
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
}
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:

KotlinJava
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.
   
}
}
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():

KotlinJava
val spinner: Spinner = findViewById(R.id.planets_spinner)
spinner
.onItemSelectedListener = this
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
spinner
.setOnItemSelectedListener(this);

如果實作 AdapterView.OnItemSelectedListenerActivityFragment 互動,如 上述的例子,您可以傳遞 this 做為介面執行個體。