RatingBar 其实也是一种 ProgressBar ,它是 ProgressBar 的间接派生类,因此 ProgressBar 可以用的方法, RatingBar 都可以用。
1. 在布局文件 (main.xml) 中,增加界面元素声明如下:
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
< RatingBar
android:id = "@+id/ratebar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:numStars = "5"
android:stepSize = "0.1"
/>
< TextView
android:id = "@+id/tv_ratebar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = ""
/>
</ LinearLayout >
2. Activity 所对应的代码:
public class ControlRatingBar extends Activity
implements
OnRatingBarChangeListener
{
private RatingBar ratebar ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super .onCreate(savedInstanceState);
setContentView(R.layout. main );
ratebar = (RatingBar)findViewById(R.id. ratebar );
ratebar .setOnRatingBarChangeListener( this );
}
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser)
{
((TextView)findViewById(R.id. tv_ratebar )).setText( " " + rating);
}
}
运行结果 :