Android RatingBar示例

In this tutorial you will learn about Android RatingBar with example.

在本教程中,您将通过示例了解Android RatingBar。

RatingBar is widget in android that is used to show user rating in the form of stars. The user can drag or touch it to set the rating value. Rating bar is very import widget specially for eCommerce apps where we have to display user rating.

RatingBar是Android中的小部件,用于以星号形式显示用户评分。 用户可以拖动或触摸它来设置额定值。 评分栏是非常导入的小部件,专门用于电子商务应用程序,我们必须在其中显示用户评分。

To use rating bar in android just add a <RatingBar> tag in layout xml file. Some of its important attributes are given below.

要在android中使用评分栏,只需在布局xml文件中添加<RatingBar>标签。 下面列出了它的一些重要属性。

AttributeJava MethodDescription
android:ratingsetRating(float rating)Used to assign default rating value.
android:numStarssetNumStars(int numStars)Used to set number of starts in rating bar. If you use this attribute then make sure rating bar width is wrap content.
android:isIndicatorsetIsIndicator(boolean)If boolean value is true then rating bar act as indicator and user can’t change its value. Rating bar is changeable if boolean value is false. By default rating bar is changeable.
android:stepSizesetStepSize(float stepSize)Used to set rate of increase and decrease. If step size is 1.0 then rating bar value will increase or decrease by 1.0.
属性 Java方法 描述
android:rating setRating(浮动评级) 用于分配默认额定值。
android:numStars setNumStars(int numStars) 用于在评级栏中设置启动次数。 如果使用此属性,请确保等级栏宽度为包装内容。
android:isIndicator setIsIndicator(boolean) 如果布尔值是true,则评分栏充当指示符,用户无法更改其值。 如果布尔值为false,则等级栏可以更改。 默认情况下,评级栏是可更改的。
android:stepSize setStepSize(float stepSize) 用于设置增加和减少的速率。 如果步长为1.0,则等级栏值将增加或减少1.0。

Use getRating() method to get the current value of rating bar. Apply setOnRatingBarChangeListener to rating bar to get rating value whenever it changes.

使用getRating()方法获取评分栏的当前值。 将setOnRatingBarChangeListener应用于评分栏,以在更改时获取评分值。

Below example show how to implement rating bar in android.

下面的示例显示了如何在android中实现评分栏。

Android RatingBar示例 (Android RatingBar Example)

Make an android studio project with package name com.androidratingbar.

创建一个名为com.androidratingbar的android studio项目

Now add following code in respective files.

现在,在相应文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:padding="15dp"
    tools:context="com.androidratingbar.MainActivity"
    android:orientation="vertical">
 
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar"
        android:stepSize="1.0"
        android:numStars="5"/>
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Your Rating: "
        android:id="@+id/ratingText"/>
</LinearLayout>

MainActivity.java

MainActivity.java

package com.androidratingbar;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
 
public class MainActivity extends AppCompatActivity {
    RatingBar ratingBar;
    TextView ratingText;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ratingBar = (RatingBar)findViewById(R.id.ratingBar);
        ratingText = (TextView)findViewById(R.id.ratingText);
 
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                ratingText.setText("Your Rating: " + rating);
            }
        });
    }
}

Save and run the project.

保存并运行项目。

When you will touch or drag your finger over rating bar then its value will be changed and displayed in textview.

当您触摸或在分级栏上拖动手指时,其值将被更改并显示在文本视图中。

Screenshot

屏幕截图

Android RatingBar Example

Comment below if you have queries or found anything incorrect in above android rating bar example.

如果您有疑问或在上面的Android评分栏示例中发现任何不正确的内容,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/12/android-ratingbar-example.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值