Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 안드로이드
- 게시판
- java#왕초보
- Android
- 숫자
- git
- 오버라이드
- degit
- parcel
- 시큐어코딩
- SQL
- snowpack
- 왕초보
- 쿠키
- 상속
- 미니게임
- React
- 답글
- FIle
- 스타일보험
- 함수
- 버전일치
- kotlin
- webpack
- sub query
- 코틀린
- 스프링
- Spring
- Spinner
- java
Archives
- Today
- Total
YSHUSH
Spinner 본문
spinner에 항목을 선택하면 텍스트 뷰에 보여주도록 하자
1. activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_width="390dp"
android:layout_height="67dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintVertical_bias="0.931"/>
<TextView
android:id="@+id/textView"
android:layout_width="269dp"
android:layout_height="64dp"
android:text="Hello World!"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintVertical_bias="0.604"/>
</androidx.constraintlayout.widget.ConstraintLayout>
2. app - main - java폴더 내 메인 패키지 폴더 우클릭 - 새로만들기 - XML - Layout XML File - item_spinner로 생성
item_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvItemSpinner"
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingTop="10dp"
android:paddingStart="30dp"
android:textColor="@android:color/black"
android:textSize="15sp"
android:paddingLeft="30dp"/>
3. app - src - main - res - values 우클릭 후 새로만들기 - XML - Values XML File - array이름으로 xml파일 생성
array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="fruit">
<item>과일선택</item>
<item>귤</item>
<item>파인애플</item>
<item>자몽</item>
<item>망고</item>
</string-array>
</resources>
4. MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupSpinnerFruit()
setupSpinnerHandler()
}
// spinner에 값을 설정하는 함수
fun setupSpinnerFruit(){
//val fruit = arrayOf("과일 선택", "사과", "배", "바나나", "포도")
val fruit = resources.getStringArray(R.array.fruit)
// 어댑터에 배열과 xml을 적용한다
val adapter = ArrayAdapter(this, R.layout.item_spinner, fruit)
// spinner에 적용
val spinner = findViewById<Spinner>(R.id.spinner)
spinner.adapter = adapter
}
// 선택시 결과를 출력해주는 함수
fun setupSpinnerHandler(){
val spinner = findViewById<Spinner>(R.id.spinner)
val textView = findViewById<TextView>(R.id.textView)
spinner.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
textView.text = "선택됨: $position ${spinner.getItemAtPosition(position)}"
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
}
}
'Coding > Android(kotlin)' 카테고리의 다른 글
Fragment (0) | 2022.02.08 |
---|---|
Spinner2 (0) | 2022.02.08 |
Context menu (0) | 2022.02.08 |
체크박스 (0) | 2022.02.08 |
라디오 버튼 (0) | 2022.02.08 |