1. RatingBarDemo.java
public class RatingBarDemo extends AppCompatActivity {
private RatingBar ratingBarA = null;
private TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar_demo);
this.ratingBarA = (RatingBar)findViewById(R.id.ratingBar1); //取得组件
this.textView = (TextView)super.findViewById(R.id.text);
this.ratingBarA.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl()); //设置监听
}
private class OnRatingBarChangeListenerImpl implements RatingBar.OnRatingBarChangeListener {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
RatingBarDemo.this.textView.append("**当前值:"+ratingBar.getRating()+
",增长步长:"+ratingBar.getStepSize()+"\n"); //增加文本
}
}
}
2. activity_rating_bar_demo.xml
<!--有5星评分,每次评分步长0.5,用户可以操作-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBarB"
android:numStars="5"
android:stepSize="0.5"
android:isIndicator="false"
android:layout_centerHorizontal="true" />
<!--默认3颗星-->
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="3"
android:id="@+id/ratingBar1"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/ratingBarB"
android:layout_alignStart="@+id/ratingBarB"
android:layout_marginTop="63dp"
android:numStars="5"
android:stepSize="0.5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_below="@+id/ratingBar1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp" />