*위젯-RatingBar
-별점바, 등급바 등으로 불리는 별점을 매기는 위젯
*레이팅바 속성
-numStars : 전체 표시되는 별의 개수 설정
-rating : 기본 별점, 처름 시작하는 별점 값
-stepSize : 별을 드래그 했을 때 움직이는 최소단위 0.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: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"
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"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.0"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar"
app:layout_constraintVertical_bias="0.05" />
</androidx.constraintlayout.widget.ConstraintLayout>
-build.gradle
-- android{} 스코프 뷰바인딩
//뷰바인딩
buildFeatures{
viewBinding true
}
-MainActivity.kt
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
//레이팅바 리스너 사용 : setOnRatingBarChangeListener {}
//두 번째 파라미터 : 현재별점
//세 번쨰 파라미터 : 사용자 입력 여부
binding.ratingBar.setOnRatingBarChangeListener { ratingBar, fl, b ->
//인터페이스 메서드를 생성한 시크바와 구조가 다르기 때문에 중괄호 안에 식 바로 사용가능
binding.textView.text="$fl"
}
//레이팅바 직접 코드로 값 입력 가능
//binding.ratingBar.rating=2.5F
}//onCreate
}//MainActivity
-리스너 사용 시 람다식 형태 사용 : 코드를 축양한 형태
-메서드의 개수가 한 개이면 메서드 이름을 작성하지 않고 중괄호를 사용해 처리할 수 있음
-결과 : 클릭하는 값에따라 텍스트 변경됨
이 포스팅에 작성한 내용은 고돈호, ⌜이것이 안드로이드다⌟, 한빛미디어(주), 2022 에서 발췌하였습니다.
'Android App > Kotlin' 카테고리의 다른 글
뷰 바인딩(View Binding) (0) | 2022.11.04 |
---|---|
위젯 사용예제-라디오버튼, 체크박스, 프로그래스바 (0) | 2022.11.03 |
위젯-SeekBar (0) | 2022.11.03 |
위젯-ProgressBar (0) | 2022.11.03 |
위젯-CheckBox, ToggleButton, Switch (0) | 2022.11.02 |