TOP
본문 바로가기
Java/Java Android

[Android] 위젯 - 평점 (RatingBar)

by 기록자_Recordian 2025. 4. 30.
728x90
반응형
이전 내용
 

[Android] 컴파운드 버튼 3 - 토글 버튼

이전 내용 [Android] 컴파운드 버튼 2 - 라디오 버튼이전 내용 [Android] 컴파운드 버튼 1 - 체크 박스이전 내용 [Android] 이벤트 리스너 예제이전 내용 [Android] 텍스트뷰 동적 생성이전 내용 [Android] 레이

puppy-foot-it.tistory.com


평점 (RatingBar)

 

RatingBar는 Android 애플리케이션에서 사용자에게 평점 또는 평가를 입력받기 위해 사용되는 UI 컴포넌트이며, 일반적으로 별 모양의 아이콘을 사용하며, 사용자는 별의 개수로 평가를 표현할 수 있다.

 

[RatingBar 속성]

  • android:numStars: 사용자가 평가할 수 있는 별의 총 개수 설정. 예: 5로 설정하면 5개별 표시.
  • android:stepSize: 사용자가 선택할 수 있는 단계의 크기 설정. 0.5로 설정하면 사용자가 반별로 평가할 수 있으며, 1로 설정하면 정수 평점만 가능.
  • android:rating: 초기 평점 값 설정. 이 속성은 float 타입으로, 기본값은 0.0.

평점 구현 예제

 

Q. 이미지의 평점을 매기고, 평점이 표시되도록 구현

 

먼저, drawable 폴더 내에 평가할 이미지를 넣어야 한다.

<LinearLayout 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:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="이 작품을 평가해 주세요"
        android:textSize="20sp"
        android:textAlignment="center" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/pic3" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:rating="2.0"
        android:stepSize="1.0" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:backgroundTint="#FF9800"
        android:text="SUBMIT" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SCORE= " />

</LinearLayout>

 

 

    <RatingBar
        android:id="@+id/ratingBar"  ▶ 식별자
        android:layout_width="wrap_content"  ▶ 내용에 맞춰 너비 조정
        android:layout_height="wrap_content"  ▶ 내용에 맞춰 높이 조정
        android:numStars="5"  ▶ 선택 가능한 총 별의 갯수
        android:rating="2.0"  ▶ 초기 평점 값
        android:stepSize="1.0" />  ▶ 평점의 단계 (1.0 이므로, 정수 1, 2, 3, 4, 5)

 

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.RatingBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private RatingBar ratingBar;
    private TextView value;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar = (RatingBar) findViewById(R.id.ratingBar);
        value = (TextView) findViewById(R.id.textView2);
        button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                float rating = ratingBar.getRating();
//                value.setText(String.valueOf("SCORE= " + rating));

                String score = "점수 = " + rating;
                value.setText(score);
            }
        });
    }
}

 

- 변수 선언:

  • RatingBar ratingBar: 사용자에게 평점을 입력받기 위한 RatingBar 객체.
  • TextView value: 사용자가 선택한 평점을 표시하기 위한 TextView 객체.
  • Button button: 사용자가 클릭할 버튼 객체.

- onClick:

  • float rating = ratingBar.getRating(): RatingBar에서 현재 선택된 평점 값을 가져옴.
  • 주석 처리된 코드 value.setText(String.valueOf("SCORE= " + rating));: 평점을 텍스트로 변경.
  • String score = "점수 = " + rating: 평점을 문자열로 포맷팅하여 score 변수에 저장.
  • value.setText(score): TextView에 "점수 = " 형식으로 평점 표시.


다음 내용

 

[Android] 인텐트(Intent) - 1: 명시적 인텐트

이전 내용 [Android] 위젯 - 평점 (RatingBar)이전 내용 [Android] 컴파운드 버튼 3 - 토글 버튼이전 내용 [Android] 컴파운드 버튼 2 - 라디오 버튼이전 내용 [Android] 컴파운드 버튼 1 - 체크 박스이전 내용 [Andr

puppy-foot-it.tistory.com

728x90
반응형