import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
public class MainHelloRatingBar extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 定义组件对象
final RatingBar rb1 = (RatingBar) findViewById(R.id.RatingBar01);
final RatingBar rb2 = (RatingBar) findViewById(R.id.RatingBar02);
final RatingBar rb3 = (RatingBar) findViewById(R.id.RatingBar03);
// 定义评分监听器
OnRatingBarChangeListener orbcl = new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
switch (ratingBar.getId()) {
case R.id.RatingBar01:
// 把第一个评分条的值取出来设置给其他评分分条
rb2.setRating(rb1.getRating());
rb3.setRating(rb1.getRating() * 2);
;// 十颗星所以乘以2
break;
case R.id.RatingBar02:
rb1.setRating(rb2.getRating());
rb3.setRating(rb2.getRating() * 2);
;
break;
case R.id.RatingBar03:
rb1.setRating(rb3.getRating() / 2);
;
rb2.setRating(rb3.getRating() / 2);
;
break;
}
}
};
// 绑定监听器
rb1.setOnRatingBarChangeListener(orbcl);
rb2.setOnRatingBarChangeListener(orbcl);
rb3.setOnRatingBarChangeListener(orbcl);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/androidd"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/RatingBar01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
默认分值设为3=""
android:rating="3" >
</RatingBar>
<RatingBar
android:id="@+id/RatingBar03"
style="?android:attr/ratingBarStyleIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
Indicator=""
中此项默认值是TRUE=""
会导致不可操作=""
在ratingBarStyleInndicator样式=""
指示器=""
星星数量设为5=""
样式=""
默认分值设为4=""
android:isIndicator="false"
android:numStars="5"
android:rating="4" >
</RatingBar>
<RatingBar
android:id="@+id/RatingBar02"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
同上=""
小星星样式=""
android:isIndicator="false"
android:numStars="10"
android:rating="8" >
</RatingBar>
</LinearLayout>