YSHUSH

Rating bar 본문

Coding/Android(kotlin)

Rating bar

코딩; 2022. 2. 8. 09:57

 

 

Rating bar를 조작하면
별의 갯수가 텍스트뷰에 보여지도록 해보자


1. activity_main.xml

<?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">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0.0"
            android:textSize="30sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.418"
            android:id="@+id/textView"/>
    <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ratingBar"
            android:stepSize="1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 2. MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val ratingBar = findViewById<RatingBar>(R.id.ratingBar)
        val textView = findViewById<TextView>(R.id.textView)

        ratingBar.setOnRatingBarChangeListener{ ratingBar, rating, fromUser->
            textView.text = "$rating"
        }
    }
}

 

 

결과물

'Coding > Android(kotlin)' 카테고리의 다른 글

File  (0) 2022.02.10
Counter(숫자 세기 미니 프로그램)  (0) 2022.02.08
Seek bar  (0) 2022.02.08
Navigation menu  (0) 2022.02.08
Fragment  (0) 2022.02.08