DBILITY

안드로이드 RatingBar 사용법 본문

android/kotlin

안드로이드 RatingBar 사용법

DBILITY 2024. 2. 28. 16:50
반응형

package com.example.myex01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.myex01.databinding.ActivityMain4Binding

class MainActivity4 : AppCompatActivity() {

    private val binding by lazy {
        ActivityMain4Binding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        var ratingDefault = 2.5f

        binding.run {
            textRating.text = ratingDefault.toString()
            ratingBar.apply {
                numStars = 5
                stepSize = 0.5f
                rating = ratingDefault
                setOnRatingBarChangeListener { _, rating, fromUser ->
                    textRating.text = if(fromUser) rating.toString() else "0.0"
                }
            }
        }
    }
}

반응형
Comments