更多精彩内容,请点击阅读:《API Demos 2.3 学习笔记》
其中,默认风格的RatingBar是我们通常使用的可以交互的,而后面两种不能进行进行交互,只能作为指示牌。设置RatingBar样式的方法是在xml布局文件中RatingBar控件内设置style:
style="?android:attr/ratingBarStyle" style="?android:attr/ratingBarStyleSmall" style="?android:attr/ratingBarStyleIndicator"
<RatingBar android:id="@+id/ratingbar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="3" android:rating="2.5" android:stepSize="0.5" />注:android:numStars="3" 属性用来设置星星总个数,必须是一个整形值,例如 100 。
2、ratingBarStyleSmall 小风格
<RatingBar android:id="@+id/small_ratingbar" style="?android:attr/ratingBarStyleSmall" android:layout_marginLeft="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" />
<RatingBar android:id="@+id/indicator_ratingbar" style="?android:attr/ratingBarStyleIndicator" android:layout_marginLeft="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" />
// 通过findViewById方法获得RatingBar对象 RatingBar mRatingBar = (RatingBar) findViewById(R.id.ratingbar); mRatingBar.setOnRatingBarChangeListener(mRatingBarChangeListener); //创建RatingBar监听器 RatingBar.OnRatingBarChangeListener mRatingBarChangeListener = new RatingBar.OnRatingBarChangeListener() { public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { // TODO 执行用户点击RatingBar后的响应动作 final int mNumStars = ratingBar.getNumStars(); //获取RatingBar总星星数 float mrating = rating; //用户点击评定后的评分(高亮的星星个数) if(fromUser){ // TODO } else{ // TODO } } };注:创建 RatingBar 监听器时,需要注意的是,3个参数以及它们对应的含义如下:
下面我们进行实例代码解析:
res-layout-ratingbar_1.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:paddingLeft="10dip" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 一个星星数为3,评分2.5个星星的默认RatingBar --> <RatingBar android:id="@+id/ratingbar1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="3" android:rating="2.5" android:stepSize="0.5" /> <!-- 一个星星数为5,评分2.5个星星的默认RatingBar 默认步长0.5--> <RatingBar android:id="@+id/ratingbar2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="2.25" android:stepSize="0.5" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip"> <TextView android:id="@+id/rating" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 一个stlye为ratingBarStyleSmall的RatingBar,不支持交互 --> <RatingBar android:id="@+id/small_ratingbar" style="?android:attr/ratingBarStyleSmall" android:layout_marginLeft="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> </LinearLayout> <!-- 一个stlye为indicator_ratingbar的RatingBar,不支持交互 --> <RatingBar android:id="@+id/indicator_ratingbar" style="?android:attr/ratingBarStyleIndicator" android:layout_marginLeft="5dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> </LinearLayout>
package com.example.android.apis.view; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; import android.widget.TextView; import com.example.android.apis.R; /** * 演示如何使用rating bar */ public class RatingBar1 extends Activity implements RatingBar.OnRatingBarChangeListener { RatingBar mSmallRatingBar; RatingBar mIndicatorRatingBar; TextView mRatingText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ratingbar_1); // 通过findViewById方法获得一个TextView对象 mRatingText = (TextView) findViewById(R.id.rating); // 通过findViewById方法获得两个用作指示牌的RatingBar对象 mIndicatorRatingBar = (RatingBar) findViewById(R.id.indicator_ratingbar); mSmallRatingBar = (RatingBar) findViewById(R.id.small_ratingbar); // 通过findViewById方法获得两个可以交互的RatingBar对象,并设置监听器 ((RatingBar)findViewById(R.id.ratingbar1)).setOnRatingBarChangeListener(this); ((RatingBar)findViewById(R.id.ratingbar2)).setOnRatingBarChangeListener(this); } public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) { final int numStars = ratingBar.getNumStars(); //显示当前星星总数多少,被选中高亮的星星数多少 mRatingText.setText( getString(R.string.ratingbar_rating) + " " + rating + "/" + numStars); //当用户点击上面可以交互的RatingBar,触发监听器,在这里更新两个作为指示牌作用的RatingBar对象的三个参数 //更新两个作为指示牌作用的RatingBar对象的星星总数 if (mIndicatorRatingBar.getNumStars() != numStars) { mIndicatorRatingBar.setNumStars(numStars); mSmallRatingBar.setNumStars(numStars); } //更新两个作为指示牌作用的RatingBar对象的星星数(被选中高亮) if (mIndicatorRatingBar.getRating() != rating) { mIndicatorRatingBar.setRating(rating); mSmallRatingBar.setRating(rating); } //更新两个作为指示牌作用的RatingBar对象的步长值 final float ratingBarStepSize = ratingBar.getStepSize(); if (mIndicatorRatingBar.getStepSize() != ratingBarStepSize) { mIndicatorRatingBar.setStepSize(ratingBarStepSize); mSmallRatingBar.setStepSize(ratingBarStepSize); } } }