공부/안드로이드(Android)

이벤트 - Rating Bar

도도-도윤 2017. 11. 7. 22:26

이벤트 - Rating Bar


이벤트 처리를 통한 Rating Bar 구현하는 방법을 소개합니다.




1. 소개




 ratingBar의 예


RatingBar는 위의 그림처럼 별로 표시된 콤포런트(Component)를 말합니다.



2. 영상




 ratingBar 구현 / 이벤트처리


171107_ratingBar.zip



3. 소스코드



<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lab.program.MainActivity">

<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"
/>
</LinearLayout>


 main.xml

 

package com.example.lab.program;

import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RatingBar ratingBar = (RatingBar)findViewById(R.id.ratingBar1);
ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
Toast.makeText(getApplicationContext(), "Rating:" + v, Toast.LENGTH_SHORT).show();
}
});

}

}

 main.java




171107_ratingBar.zip
4.9MB