Android的开发中,有一个叫做评分控件RatingBar,我们可以使用该控件做等级划分、评分等作用,星星形状显示,也可以半星级别,我们来看一下评分控件如何使用。

布局文件中定义控件以及属性,这里主要需要指定的是总星星数量,和当前的值,也就是总级别跟当前级别的量。

<RatingBar
   android:id="@+id/ratingBar"
   android:numStars="5" //总级别,总分,星星个数
   android:rating="1.5"  //当前级别,分数,星星个数
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
</RatingBar>

评分控件中两个比较重要的方法:

RatingBar.setRating(flaot rating);
RatingBar.getRating();

事件监听处理:

RatingBar.setOnRatingBarChangeListener(newOnRatingBarChangeListener(){
@Override
    public voidonRatingChanged(RatingBar ratingBar, floatrating,  boolean fromUser) {
          //doing actions
    }
});

本文出自http://1622511.blog.51cto.com/1612511/567316

Android 中文 API  ——RatingBar

正文

  一、结构

    publicclass RatingBar extends AbsSeekBar

    Java.lang.Object

    android.view.View

    android.widget.ProgressBar

                 android.widget.AbsSeekBar

  android.widget.RatingBar

  二、概述

    

  RatingBar是基于SeekBar和ProgressBar的扩展,用星型来显示等级评定。使用RatingBar的默认大小时,用户可以触摸/拖动或使用键来设置评分,它有两种样式(小风格用ratingBarStyleSmall,大风格用ratingBarStyleIndicator),其中大的只适合指示,不适合于用户交互。

  当使用可以支持用户交互的RatingBar时,无论将控件(widgets)放在它的左边还是右边都是不合适的。

  只有当布局的宽被设置为wrap content时,设置的星星数量(通过函数setNumStars(int)或者在XML的布局文件中定义)将显示出来(如果设置为另一种布局宽的话,后果无法预知)。

  次级进度一般不应该被修改,因为他仅仅是被当作星型部分内部的填充背景。

  参见FormStuff tutorial.

  三、嵌套类

  接口:RatingBar.OnRatingBarChangeListener

  一个回调函数,当星级进度改变时修改客户端的星级。

  四、XML属性

属性名称

描述

android:isIndicator

RatingBar是否是一个指示器(用户无法进行更改)

android:numStars

显示的星型数量,必须是一个整形值,像“100”。

android:rating

默认的评分,必须是浮点类型,像“1.2”。

android:stepSize

评分的步长,必须是浮点类型,像“1.2”。

  五、公共方法

public int getNumStars ()

    返回显示的星型数量

      返回值

  显示的星型数量

  public RatingBar.OnRatingBarChangeListenergetOnRatingBarChangeListener ()

返回值

监听器(可能为空)监听评分改变事件

  public float getRating ()

  获取当前的评分(填充的星型的数量)

  返回值

  当前的评分

  public float getStepSize ()

  获取评分条的步长

  返回值

  步长

  public boolean isIndicator ()

返回值

判断当前的评分条是否仅仅是一个指示器(注:即能否被修改)

  public void setIsIndicator (booleanisIndicator)

  设置当前的评分条是否仅仅是一个指示器(这样用户就不能进行修改操作了)

  参数

  isIndicator     Bool值,是否是一个指示器

  public synchronized void setMax (intmax)

  设置评分等级的范围,从0到max

  参数

  max       评分条最大范围。

  public void setNumStars (intnumStars)

  设置显示的星型的数量。为了能够正常显示它们,建议将当前widget的布局宽度设置为

wrap content

  参数

  numStars       星型的数量

  public void setOnRatingBarChangeListener(RatingBar.OnRatingBarChangeListener listener)

  设置当评分等级发生改变时回调的监听器

  参数

  listener 监听器

  public void setRating (floatrating)

  设置分数(星型的数量)

  参数

  rating    设置的分数

  public void setStepSize (floatstepSize)

  设置当前评分条的步长(step size)

  参数

  stepSize 评分条的步进。例如:如果想要半个星星,它的值为0.5。

  六、受保护方法

  protectedsynchronized void onMeasure (intwidthMeasureSpec, int heightMeasureSpec)

  权衡 view和 content来决定它的宽度和高度的整齐。它被measure(int,int) 调用 并且应该被子类所覆盖,以便提供准确高效的布局测量。

  规定:当覆盖这个方法的时候,你必须调用setMeasuredDimension(int,int)以便存储精确的视图的宽和高。如果不这样做的话将触发llegalStateException异常,被函数measure(int,int)抛出。调用父类onMeasure(int,int)是合理的。

  尺寸的基本类的实现默认是背景大小,除非通过MeasureSpec允许大的尺寸。子类应该覆盖onMeasure(int, int)以便提供更好的布局大小。

如果这个方法被覆盖,子类应该负责确保标准的宽和高至少是视图的最小宽度和高度的值(分别为getSuggestedMinimumHeight()和 getSuggestedMinimumWidth()两方法)。

    参数

    widthMeasureSpec    受主窗口支配的水平空间要求。这个需求通过View.MeasureSpec.进行编码。

      heightMeasureSpec  受主窗口支配的垂直空间要求。这个需求通过View.MeasureSpec.进行编码。

  七、补充

    文章链接

Android控件之RatingBar评分条

Android更换RatingBar图片

[Android学习指南]RatingBar评分条

    示例代码(代码转载自Android手机开发者论坛

Java文件

 
public class AndroidRatingBar extends Activity {
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

final RatingBar ratingBar_Small = (RatingBar)findViewById(R.id.ratingbar_Small);
       final RatingBar ratingBar_Indicator = (RatingBar)findViewById(R.id.ratingbar_Indicator);
       final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default);

ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){

public void onRatingChanged(RatingBar ratingBar, float rating,
     boolean fromUser) {
    ratingBar_Small.setRating(rating);
    ratingBar_Indicator.setRating(rating);
    Toast.makeText(AndroidRatingBar.this, "rating:"+String.valueOf(rating),
      Toast.LENGTH_LONG).show();
   }});
   }
}

 

XML文件

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<RatingBar 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   style="?android:attr/ratingBarStyleIndicator"
   android:id="@+id/ratingbar_Indicator"
   />
<RatingBar 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   style="?android:attr/ratingBarStyleSmall"
   android:id="@+id/ratingbar_Small"
   android:numStars="20"
   />
<RatingBar 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   style="?android:attr/ratingBarStyle"
   android:id="@+id/ratingbar_default"
   />
</LinearLayout>
 

结束

  本文为"madgoat"和"wallace2010"联合署名,原因是翻译重了,不过两个翻译得都很好,这样翻译重的事件也将随着管理的完善得意解决,感谢两位的相互理解,感谢大家的支持!

http://www.cnblogs.com/over140/archive/2010/11/18/1880391.html

 

Android星星评分控件RatingBar的使用的更多相关文章

  1. Android自定义评分控件:RatingStarView

    RatingStarView Android自定义的评分控件,类似ProgressBar那样的,使用星星图标(full.half.empty)作为progress标识的评分/打分控件. 效果图 图1: ...

  2. iOS 类似美团或饿了么评价中的星星评分控件

    1.做的好几个项目都用到了评分控件,可以用来展示评分,也可以用来写评分,图片和间距大小都可以定制,之前就已经简单封装了一个,现在把它分享出来,有需要的拿去用. 2.下面是展示截图:   image.p ...

  3. Android 拖动条/滑动条控件、星级评分控件

    ProgressBar有2个子控件: SeekBar   拖动条控件 RatingBar   星级评分控件 1.拖动条控件 <SeekBar android:layout_width=" ...

  4. 自定义RatingBar评分控件

    1.介绍 实现类似美团外卖评分供能,系统提供了RatingBar,今天来自定义一波,当做自定义view的一个学习,效果如下,能够滑动或者点击变化星星数量 2.自定义属性 在values目录下的attr ...

  5. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  6. JQuery版评分控件

    Hi All, 分享一个学习JQuery做的一个评分控件. 需求:当鼠标移动到 ‘☆’ 上时,该字符左边的 ‘☆’ 变成 '★',该字符右边仍然是 ‘☆’, 并显示相应星星数的评价结果:当鼠标推出 ‘ ...

  7. Web用户控件开发--星型评分控件

    本文中分享一个实现简单,使用方便的星型评分控件. 一:贴几张测试图片先: 二.星型评分控件的实现: RatingBar.ascx: <%@ Control Language="C#&q ...

  8. UWP开发---DIY星级评分控件

    一,需求来源 在开发韩剧TV UWP过程中,遇到了星级评分的控件问题,在安卓和html中很容易用现有的轮子实现星级评分,搜索了一下目前UWP还未有相关文章,在WPF的一篇文章中使用Photo shop ...

  9. iOS- 非整星的评分控件(支持小数)

    概述 订单评论里实现星级评分控件: 简单整星评价与非整星的精评价. 详细 代码下载:http://www.demodashi.com/demo/10711.html 现在很多应用都有评分功能. 有了订 ...

随机推荐

  1. c# UDP通信

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. texconv下载以及使用命令

    包含texconv.exe,测试图片和测试批处理文件 命令: texconv.exe -ft DDS .\src\*.bmp -o .\output\ 下载: http://files.cnblogs ...

  3. ip地址定位库

    ip2region 1.2.1 发布了,新增 Python 内存查询+数据文件更新. 准确率99.9%的ip地址定位库,0.0x毫秒级查询,数据库文件大小只有1.5M,提供了java, php, c, ...

  4. C#中求数组的子数组之和的最大值

    <编程之美>183页,问题2.14——求子数组的字数组之和的最大值.(整数数组) 我开始以为可以从数组中随意抽调元素组成子数组,于是就有了一种想法,把最大的元素抽出来,判断是大于0还是小于 ...

  5. Android中直播视频技术探究之---视频直播服务端环境搭建(Nginx+RTMP)

    一.前言 前面介绍了Android中视频直播中的一个重要类ByteBuffer,不了解的同学可以 点击查看 到这里开始,我们开始动手开发了,因为我们后续肯定是需要直播视频功能,然后把视频推流到服务端, ...

  6. ByteBuffer解析

    一.前言 前一篇文章我们介绍了Android中直播视频技术的基础大纲知识,这里就开始一一讲解各个知识点,首先主要来看一下视频直播中的一个重要的基础核心类:ByteBuffer,这个类看上去都知道了,是 ...

  7. EF 存储过程

    今天我们利用EF执行sql语句的方式来执行存储过程,并得到OutPut的值. 首先新建存储过程: Create PROCEDURE proc_testEF   (     @id int,     @ ...

  8. DbUtils使用时抛出Cannot get a connection

    java.sql.SQLException: Cannot get a connection, pool error Timeout waiting for idle object Caused by ...

  9. asynchronous vs non-blocking

    http://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking In many circumstances they a ...

  10. sqlmap基本命令

    ./sqlmap.py –h //查看帮助信息./sqlmap.py –u “http://www.anti-x.net/inject.asp?id=injecthere” //get注入./sqlm ...