什么是RatingBar?
RatingBar是评分进度条。
如何使用RatingBar?
1.在布局文件中声明RatingBar标签,并设置numStars属性和stepSize属性。
2.在Activity中创建RatingBar对象。
3.为RatingBar对象创建监听器类实现OnRatingBarChangeListener类,并重写onRatingChanged()方法。
4.为RatingBar绑定监听器。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mycompany.ratingbar.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请评分:" />
<RatingBar
android:id="@+id/ratingBar"
android:layout_below="@id/textView"
android:numStars="5"
android:stepSize="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
package com.mycompany.ratingbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
public class MainActivity extends AppCompatActivity {
private RatingBar ratingBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
ratingBar.setOnRatingBarChangeListener(new RatingBarListener());
}
class RatingBarListener implements RatingBar.OnRatingBarChangeListener{
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
System.out.println("rating------>" + rating);
}
}
}