当前位置: 首页 > 编程笔记 >

Android星级评分条的实现代码

松和泰
2023-03-14
本文向大家介绍Android星级评分条的实现代码,包括了Android星级评分条的实现代码的使用技巧和注意事项,需要的朋友参考一下

RatingBar星级评分条

它跟拖动条类似。都允许用户拖动来改变进度,不同的是,星级评分条通过星星图案表示进度。想淘宝,等等都有这个东西。很简单。

重要记一个组件<RatingBar 属性列表></RatingBar>,属性见名知意
还有三个方法:
1、getRating()方法:获取等级,表示你当前选中了几颗星星
2、getStepSize()方法:每次最少可以改变多少星星(默认是0.5个)
3、getProgress()方法:获取进度,是前两个之积。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:app="http://schemas.android.com/apk/res-auto" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  tools:context="com.amy.ratingbartest.MainActivity"> 
 
  <LinearLayout 
    android:orientation="vertical" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    tools:layout_editor_absoluteY="0dp" 
    tools:layout_editor_absoluteX="8dp"> 
    <!-- 星级评分条 --> 
    <RatingBar 
      android:id="@+id/ratingBar1" 
      android:numStars="5" 
      android:rating="2.5" 
      android:isIndicator="false" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
 
    <!-- 按钮 --> 
    <Button 
      android:id="@+id/button1" 
      android:text="提交" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
 
  </LinearLayout> 
 
</android.support.constraint.ConstraintLayout> 

MainActivity.java

package com.amy.ratingbartest; 
 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.RatingBar; 
import android.widget.Toast; 
 
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.ratingBar1); 
    Button button = (Button) findViewById(R.id.button1); 
    button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
        /** 
         * getRating():用于获取等级,表示选中的几颗星 
         * getStepSize():用语获取每次至少要改变多少个星级 
         * getProgress():用语获取进度,获取到的进度值为getRating()方法返回值与getStepSize()方法返回值之积 
         */ 
        int result = ratingBar.getProgress(); 
        float rating = ratingBar.getRating(); 
        float step = ratingBar.getStepSize(); 
        Log.e("星级评分条","step="+step+"result="+result+"rating="+rating); 
        Toast.makeText(MainActivity.this,"你得到了"+rating+"颗星",Toast.LENGTH_SHORT).show(); 
      } 
    }); 
  } 
} 

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍Android实现根据评分添加星级条,包括了Android实现根据评分添加星级条的使用技巧和注意事项,需要的朋友参考一下 简述 在仿写豆瓣的时候,发现了根据评分不同,星级数也不同的星级条。 百度一搜,发现Android有自带控件UIRatingBar,而iOS得要自己写…好吧,那就写吧。 图片素材 首先,要准备三张图片,图片如下: 空星,半星,全星 因为我们可以看到,在豆瓣的评分星级

  • 本文向大家介绍使用jQuery实现星级评分代码分享,包括了使用jQuery实现星级评分代码分享的使用技巧和注意事项,需要的朋友参考一下 前面有一篇原生js实现星级评分 。可能覆盖面不是很广,现在给出一个jquery实现的星级评分。   使用方法超级简单,这里就不多废话了,小伙伴们拿走自由发挥吧。

  • 主要内容:本节引言:,1.RatingBar基本使用:,2.定制环节:,本节小结:本节引言: 上一节的SeekBar是不是很轻松咧,本节我们学的这个RatingBar(星级评分条)也非常简单,相信在某宝, 买过东西的对这个应该不陌生,收到卖家的包裹,里面很多时候会有个小纸片,五星好评返还多少元这样, 而评分的时候就可以用到我们这个星级评分条了~先来瞅瞅官方文档 官方文档:RatingBar 我们可以看到,这玩意和SeekBar的类结构是一样的,也是ProgressBar的子类:

  • 本文向大家介绍Android RatingBar星星评分控件实例代码,包括了Android RatingBar星星评分控件实例代码的使用技巧和注意事项,需要的朋友参考一下 效果图: 直接上代码: xml文件: Java代码: 以上所述是小编给大家介绍的Android RatingBar 评分控件,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网

  • Rate 组件使用 html5 中 data- 属性实现,当点击评分区域时,获取 data 属性的值,然后通过 vue 中 :class 控制评分星星的状态,即是否为 active。 <template> <div class="page__bd"> <div class="weui-cells__title">点击评分</div> <div class="weui

  • 本文向大家介绍Android星级评分条控件RatingBar使用详解,包括了Android星级评分条控件RatingBar使用详解的使用技巧和注意事项,需要的朋友参考一下 Android开发中,时不时的就有要实现星星的评分效果,比如某宝,某团,相信大家也都见过,当然了我们可以自己去画,也可以用美工给切的图去实现,其实在Android原生的控件中就可以来实现这样的效果,它就是RatingBar。 来