앱에 스피너 추가
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
스피너는 값 집합에서 하나의 값을 선택할 수 있는 빠른 방법을 제공합니다. 기본값
스피너에 현재 선택된 값이 표시됩니다. 스피너 탭하기
사용자가 선택할 수 있는 다른 모든 값을 보여주는 메뉴를 표시합니다.
그림 1. 사용 가능한 메뉴가 표시된 스피너 메뉴
값으로 사용됩니다.
다음과 같이 레이아웃에 스피너를 추가할 수 있습니다.
Spinner
객체를 만들 수 있습니다. 이 작업은 일반적으로 XML 레이아웃에서
<Spinner>
요소 이 내용은 다음에 나와 있습니다.
예:
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
스피너를 선택 목록으로 채우려면
SpinnerAdapter
내
Activity
또는
<ph type="x-smartling-placeholder">Fragment
</ph>
소스 코드
Material Design 구성요소를 사용하는 경우
노출
드롭다운 메뉴는 Spinner
와 같습니다.
사용자가 선택할 수 있는 스피너 채우기
스피너에 대해 제공하는 선택은 모든 소스에서 가져올 수 있지만
다음과 같은 SpinnerAdapter
를 통해 제공해야 합니다.
ArrayAdapter
선택 항목이 배열 또는
<ph type="x-smartling-placeholder">CursorAdapter
</ph>
선택 항목을 사용할 수 있는지
확인할 수 있습니다
예를 들어 스피너에 사용할 수 있는 선택 항목이 미리 결정되어 있다면
에 정의된 문자열 배열을 제공하여
문자열 리소스
파일에 저장합니다.
<?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
}
자바
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.
}
}
자바
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()
및
<ph type="x-smartling-placeholder">onNothingSelected()
</ph>
콜백 메서드를 지원합니다.
다음을 호출하여 인터페이스 구현을 지정합니다.
setOnItemSelectedListener()
:
Kotlin
val spinner: Spinner = findViewById(R.id.planets_spinner)
spinner.onItemSelectedListener = this
자바
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
spinner.setOnItemSelectedListener(this);
AdapterView.OnItemSelectedListener
를 구현하는 경우
다음과 같이 Activity
또는 Fragment
인터페이스를 사용합니다.
이전 예에서 this
를 인터페이스 인스턴스로 전달할 수 있습니다.
이 페이지에 나와 있는 콘텐츠와 코드 샘플에는 콘텐츠 라이선스에서 설명하는 라이선스가 적용됩니다. 자바 및 OpenJDK는 Oracle 및 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."]]