acvitity_ratingbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_rating_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jump.RatingBarActivity"
android:orientation="vertical">
<!--默认五颗星-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--指示器为true,不可操作-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"/>
<!--指示器为false,可操作-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"/>
<!--指示器为false,可操作,设置默认星数为6,默认步长为0.5-->
<RatingBar
android:id="@+id/rb_rat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="false"
android:numStars="6"
android:stepSize="0.5"/>
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25sp"/>
</LinearLayout>
RatingBarAcvitity.java
package com.example.jump;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class RatingBarActivity extends AppCompatActivity {
private RatingBar rb_rat;
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar);
intiView();
}
/**
* rating 点击后 的星数
* fromUser 是否是用户点击的星数
*/
private void intiView(){
//给评分条添加点击事件
rb_rat= (RatingBar) findViewById(R.id.rb_rat);
tv_result= (TextView) findViewById(R.id.tv_result);
rb_rat.setRating((float) 1.5);//设置默认初始化星数
rb_rat.setNumStars(6);//设置总星数
rb_rat.setStepSize((float) 1.5);//设置默认步长
rb_rat.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
tv_result.setText("总星数:"+ratingBar.getNumStars()+""+" ,方法星数:"+ratingBar.getRating()+" ,步长:"+ratingBar.getStepSize()+",当前监听进度"+rating);
}
});
}
}