1.在布局文件中声明一个RatingBar
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0"/>
2.定义一个onRatingBarListener
private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
System.out.println("rating---->"+rating);
}
}
3.完整代码
package com.example.seekbardemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.SeekBar;
public class MainActivity extends Activity {
private RatingBar ratingBar=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar)findViewById(R.id.ratingbar);
ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
}
private class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
System.out.println("rating---->"+rating);
}
}
}