在main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar //评分组件
android:id="@+id/ratingbarA"
android:numStars="5" //一共5颗星
android:stepSize="0.5" //每次操作的歩长是0.5个
android:isIndicator="false" //用户可以操作
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<RatingBar
android:id="@+id/ratingbarB"
android:numStars="5"
android:rating="3" //默认值为3颗星
android:isIndicator="true" //用户不可以操作
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView //以后对于分数改变是时候可以再这个组件上显示
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在MyRatingBarDemo.java程序中
package com.tarena.ratingbar;
import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MyRatingBarDemo extends Activity {
private RatingBar ratingBarA = null;
private TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.ratingBarA = (RatingBar) super.findViewById(R.id.ratingbarA);
this.text = (TextView) super.findViewById(R.id.text);
this.ratingBarA.setOnRatingBarChangeListener(new OnRatingBarChangeListenerImpl());
this.text.setMovementMethod(ScrollingMovementMethod.getInstance()); //设置文本可滚动
}
private class OnRatingBarChangeListenerImpl implements OnRatingBarChangeListener{
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
MyRatingBarDemo.this.text.append("当前值为: " + ratingBar
.getRating() + " , 歩长为: " + ratingBar.getStepSize() + "\n");
}
}
}