[안드로이드 스튜디오/코틀린] 서치 뷰, 스피너
SearchView
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<SearchView
android:id="@+id/searchView"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.SearchView
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
private var MyList = arrayOf("yeji", "ryujin", "chaeryeong", "yuna", "lia")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.searchView.setOnQueryTextListener(object:SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(p0: String?): Boolean {
println(p0 + " 입력완료")
return true
}
override fun onQueryTextChange(p0: String?): Boolean {
if(p0 != "") {
val curList = MyList.filter { x -> x.toLowerCase().contains(p0?.toLowerCase().toString()) }
for (i in curList) println(i)
println("----------")
}
return true
}
})
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
Spinner
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
var items = arrayOf("나연", "정연", "모모", "사나", "지효", "미나", "다현", "채영", "쯔위")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val myAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, items)
binding.spinner.adapter = myAdapter
binding.spinner.onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
println(p2)
items[p2]
}
override fun onNothingSelected(p0: AdapterView<*>?) {
println("선택 안 함")
}
}
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Tool Bar (0) | 2022.05.12 |
---|---|
[Android Studio/Kotlin] Guide Line (0) | 2022.05.11 |
[Android Studio/Kotlin] Seek Bar, Rating Bar (0) | 2022.05.10 |
[Android Studio/Kotlin] Progress Bar (0) | 2022.05.09 |
[Android Studio/Kotlin] Views (2) Video View, Calendar View (0) | 2022.05.07 |