Android星级评分条的使用

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@mipmap/bg"
    tools:context="com.example.first.day_10_16ratingbar.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <RatingBar
            android:layout_marginTop="100dp"
            android:id="@+id/rating"
            android:numStars="5"
            android:stepSize="1"
            android:rating="1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_marginTop="110dp"
            android:id="@+id/text"
            android:text="不满意"
            android:textSize="22sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

MainActivity.java

package com.example.first.day_10_16ratingbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;

public class MainActivity extends Activity {
    RatingBar ratingBar;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar = this.findViewById(R.id.rating);
        textView  = this.findViewById(R.id.text);

        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                if(rating==1)
                    textView.setText("不满意");
                else if(rating>=2&&rating<=4){
                    textView.setText("满意");
                }else{
                    textView.setText("非常满意");
                }
            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/tdl081071tdy/article/details/83185678