【译】Android RatingBar

292 阅读1分钟

原文链接:magdamiu.com/2019/03/01/…

作者: MAGDA MIU

根据官方文档,Rating Bar是Seek Bar和ProgressBar的扩展,显示星级。

img

代码:

<RatingBar
    android:id="@+id/rating_bar_basic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:numStars="5"
    android:rating="3.5"
    android:stepSize="0.5" />

结果:

只读评级栏

它仅用于显示当前评级。如果“is Index ator”为假,它将允许用户编辑评级。

代码:

<RatingBar
    android:id="@+id/rating_bar_small"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/space_16"
    android:isIndicator="false"
    android:numStars="5"
    android:rating="3.5"
    android:stepSize="0.5" />

结果:

小评级

默认情况下,它的行为就像指标一一样,但是如果“指标”为假,它将允许用户编辑评级。

代码:

<RatingBar
    android:id="@+id/rating_bar_small"
    style="@style/Widget.AppCompat.RatingBar.Small"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/space_16"
    android:isIndicator="false"
    android:numStars="5"
    android:rating="3.5"
    android:stepSize="0.5" />

结果(第三个):

RatingBar更改监听器

代码:

RatingBar ratingBar = findViewById(R.id.rating_bar_basic);

ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        Toast.makeText(RatingBarActivity.this,
                getString(R.string.current_rating) + ratingBar.getRating(),
                Toast.LENGTH_SHORT).show();
    }
});

结果: