개발(Android)/android studio

[Android Studio/Kotlin] Seek Bar, Rating Bar

shinyelee 2022. 5. 10. 16:29

[안드로이드 스튜디오/코틀린] 시크 바, 레이팅 바

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()
    }

}

thumb을 클릭 또는 드래그해
움직일 수 있음
이렇게 출력됨

 

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()
    }

}

클릭 또는 드래그해 별점을 조정할 수 있음


참고

반응형