เพิ่มไอคอนหมุนในแอป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ไอคอนหมุนจะช่วยให้คุณเลือก 1 ค่าจากชุดได้อย่างรวดเร็ว เป็นค่าเริ่มต้น
สถานะ ไอคอนหมุนจะแสดงค่าที่เลือกไว้ในปัจจุบัน การแตะไอคอนหมุน
แสดงเมนูที่แสดงค่าอื่นๆ ทั้งหมดที่ผู้ใช้สามารถเลือกได้
รูปที่ 1. เมนูจากไอคอนหมุนแสดง
คุณสามารถเพิ่มไอคอนหมุนในเลย์เอาต์ได้ด้วย
Spinner
ซึ่งมักทำในเลย์เอาต์ XML ด้วย
องค์ประกอบ <Spinner>
ซึ่งจะแสดงในส่วนต่อไปนี้
ตัวอย่าง:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
หากต้องการเติมรายการตัวเลือกลงในไอคอนหมุน ให้ระบุ
SpinnerAdapter
ใน
Activity
หรือ
Fragment
ซอร์สโค้ด
หากคุณใช้คอมโพเนนต์ดีไซน์ Material
เปิดเผย
เมนูแบบเลื่อนลงเทียบเท่ากับ 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
จากอาร์เรย์สตริง
อาร์กิวเมนต์ที่ 3 สำหรับเมธอดนี้คือทรัพยากรเลย์เอาต์ที่กำหนดวิธี
ตัวเลือกที่เลือกไว้จะปรากฏในตัวควบคุมไอคอนหมุน แพลตฟอร์มจะให้บริการ
simple_spinner_item
เลย์เอาต์ นี่คือการจัดวางเริ่มต้น เว้นแต่คุณต้องการกำหนดการออกแบบของคุณเองสำหรับ
ลักษณะของไอคอนหมุน
โทร
setDropDownViewResource(int)
เพื่อระบุเลย์เอาต์ที่อะแดปเตอร์จะใช้เพื่อแสดงรายการตัวเลือกไอคอนหมุน
simple_spinner_dropdown_item
เป็นเลย์เอาต์มาตรฐานอีกแบบหนึ่งที่กำหนดโดยแพลตฟอร์ม
โทร
setAdapter()
เพื่อใช้อะแดปเตอร์กับ Spinner
ตอบสนองต่อการเลือกของผู้ใช้
เมื่อผู้ใช้เลือกรายการจากเมนูของไอคอนหมุน
วัตถุ Spinner
รายการ
ได้รับเหตุการณ์ในรายการที่เลือก
หากต้องการกำหนดเครื่องจัดการเหตุการณ์การเลือกสำหรับไอคอนหมุน ให้ใช้
AdapterView.OnItemSelectedListener
อินเทอร์เฟซและมิติข้อมูล
onItemSelected()
Callback Method ลองดูตัวอย่างการใช้งานอินเทอร์เฟซใน
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()
เมธอด Callback
ระบุการใช้งานอินเทอร์เฟซโดยการเรียกใช้
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 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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."]]