XML属性
1.android:isIndicator 是否允许改变,true为步允许
2.android:numStars 星星总数
3.android:rating 默认星级
4.android:stepSize 每一次改变的星星数
常用方法
getRating(); 获取等级
getStepSize(); 每次改变多少个星星
getProgress(); 获取进度值,进度值为=星星等级*每次改变的星星数
例子
<RatingBar
android:isIndicator="false"
android:numStars="5"
android:rating="3.5"
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_below="@+id/ratingBar"
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
private Button bn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
bn = (Button) findViewById(R.id.bn);
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int result = ratingBar.getProgress();
float raing = ratingBar.getRating();
float step = ratingBar.getStepSize();
Log.i("星级评分","'进度值:"+result+"默认星:"+raing+"每一步多少:"+step);
Toast.makeText(MainActivity.this, "您获得了"+raing+"颗星星", Toast.LENGTH_SHORT).show();
}
});
}
}