[안드로이드 스튜디오/코틀린] 시크 바, 레이팅 바
SeekBar
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">
<SeekBar
android:id="@+id/seekBar"
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"
android:progress="0" />
<!-- max값 100으로 고정, progress로 기본값 설정 -->
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.shinyelee.android_study2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.SeekBar
import com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
println(p1)
}
override fun onStartTrackingTouch(p0: SeekBar?) {
println("움직임 시작")
}
override fun onStopTrackingTouch(p0: SeekBar?) {
println("움직임 끝")
}
})
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
SeekBar (Discrete)
activity_main.xml
// seekBar를 seekBar2로 바꿔줌
<SeekBar
android:id="@+id/seekBar2"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:max="12"
android:progress="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- android:progress 범위 0~max 내에서 설정 가능 -->
MainActivity.kt
// seekBar를 seekBar2로 바꿔줌
binding.seekBar2.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
println(p1)
}
override fun onStartTrackingTouch(p0: SeekBar?) {
println("움직임 시작")
}
override fun onStopTrackingTouch(p0: SeekBar?) {
println("움직임 끝")
}
})
RatingBar
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">
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
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 com.shinyelee.android_study2.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private var vBinding : ActivityMainBinding? = null
private val binding get() = vBinding!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
vBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.ratingBar.setOnRatingBarChangeListener { ratingBar, fl, b ->
println(fl)
}
binding.ratingBar.rating = 3.5f
// 5점만점에 기본값 3.5로 시작
}
override fun onDestroy() {
vBinding = null
super.onDestroy()
}
}
참고
반응형
'개발(Android) > android studio' 카테고리의 다른 글
[Android Studio/Kotlin] Guide Line (0) | 2022.05.11 |
---|---|
[Android Studio/Kotlin] Search View, Spinner (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 |
[Android Studio/Kotlin] Views (1) Image View, Web View (0) | 2022.05.05 |