Android 中文 API (40) —— RatingBar

前言

  本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 r1,翻译来自"madgoat"和"wallace2010",欢迎大家访问他们的博客:http://madgoat.cn/http://blog.csdn.net/springiscoming2008,再次感谢"madgoat"和"wallace2010" !期待你加入Android中文翻译组,联系我over140@gmail.com。

声明

  欢迎转载,但请保留文章原始出处:)

    博客园:http://www.cnblogs.com/

    Android中文翻译组:http://www.cnblogs.com/over140/

正文

  一、结构

    public class 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的布局文件中定义)将显示出来(如果设置为另一种布局宽的话,后果无法预知)。

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

  参见Form Stuff tutorial.

  三、嵌套类

  接口:RatingBar.OnRatingBarChangeListener

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

  四、XML属性

属性名称

描述

android:isIndicator

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

android:numStars

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

android:rating

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

android:stepSize

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

  五、公共方法

public int getNumStars ()

    返回显示的星型数量

      返回值

  显示的星型数量

  public RatingBar.OnRatingBarChangeListener getOnRatingBarChangeListener ()

返回值

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

  public float getRating ()

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

  返回值

  当前的评分

  public float getStepSize ()

  获取评分条的步长

  返回值

  步长

  public boolean isIndicator ()

返回值

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

  public void setIsIndicator (boolean isIndicator)

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

  参数

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

  public synchronized void setMax (int max)

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

  参数

  max         评分条最大范围。

  public void setNumStars (int numStars)

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

wrap content

  参数

  numStars         星型的数量

  public void setOnRatingBarChangeListener (RatingBar.OnRatingBarChangeListener listener)

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

  参数

  listener  监听器

  public void setRating (float rating)

  设置分数(星型的数量)

  参数

  rating      设置的分数

  public void setStepSize (float stepSize)

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

  参数

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

  六、受保护方法

  protected synchronized void onMeasure(int widthMeasureSpec, 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 {
   /** Called when the activity is first created. */
   @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>

Android 中文 API (40) —— RatingBar的更多相关文章

  1. Android 中文API (69) —— BluetoothAdapter[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...

  2. Android 中文API (70) —— BluetoothDevice[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android  2.3 ...

  3. Android 中文API (68) —— BluetoothClass.Service

    前言 本章内容是 android.bluetooth.BluetoothClass.Service,为Android蓝牙部分的章节翻译,版本为 Android 2.3   r1,翻译来自中山大学的&q ...

  4. Android 中文API (67) —— BluetoothClass.Device.Major

    前言 本章内容是android.bluetooth.BluetoothClass.Device.Major,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学 ...

  5. Android 中文API (66) —— BluetoothClass.Device

    前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学的" ...

  6. Android 中文API (65) —— BluetoothClass[蓝牙]

    前言 本章内容是android.bluetooth.BluetoothClass,为Android蓝牙部分的章节翻译.用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法 ...

  7. android中文api(79)——Gallery

    前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www. ...

  8. Android中文API

    Android中文API http://www.android-doc.com/index.html

  9. Android 中文 API (29) —— CompoundButton

    前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...

随机推荐

  1. Android学习手记(3) Activity间传递数据

    1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...

  2. Android弹性ScrollView

    开袋即食 import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; ...

  3. IPTV中的EPG前端优化

    先看一下IPTV相关情况: l 目前TPTV市场情况 a) 截止今年2月,全国IPTV总用户数达3630.2万,我国移动互联网用户规模接近9亿,人均月接入量近300M,8M宽带达半数,光纤近4成. 图 ...

  4. RMAN-06496: must use the TO clause when the database is mounted or open

    一:问题描述 备份控制文件时报错: RMAN> restore controlfile from '/home/oracle/backup/PROD_32_20140829.bak'; Star ...

  5. 武汉科技大学ACM:1007: 陶陶摘苹果

    Problem Description 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试. 个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹 ...

  6. cos实现文件上传--推荐

    1.导包 2.核心类:MultipartRequest MultipartRequest是request的包装类 参数1:request 参数2:保存路径 参数3:上传文件大小,默认为1M 参数4:编 ...

  7. Oracle数据库简介

    Oracle数据库简介 一.介绍 Oracle数据库系统是美国Oracle(甲骨文)公司提供的以分布式数据库为核心的一组软件产品,是目前最流行的客户/服务器(Client/Server,C/S)或浏览 ...

  8. linux定时任务crond那些事!

    1.定时任务crond介绍 1.1 crond是什么 crond是linux系统中用来定期执行命令或指定程序任务的一种服务或软件. 特殊需求:(秒级别)crond服务就无法搞定了,一般工作中写脚本守护 ...

  9. 命令passwd报错因inode节点处理记录

    命令passwd报错因inode节点处理记录故障现象:1.修改密码时报错 passwd: Authentication token manipulation error2.添加用户报错:unable ...

  10. js中给函数传参函数时,函数加括号与不加括号的区别

    <!doctype html><html><head><script> function show() { alert("123") ...