Android 中文 API (40) —— RatingBar

來源:互聯網
上載者:User

前言

  本章內容是 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檔案

 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public class AndroidRatingBar extends Activity { 2    /** Called when the activity is first created. */ 3    @Override 4    public void onCreate(Bundle savedInstanceState) { 5        super.onCreate(savedInstanceState); 6        setContentView(R.layout.main); 7  8        final RatingBar ratingBar_Small = (RatingBar)findViewById(R.id.ratingbar_Small); 9        final RatingBar ratingBar_Indicator = (RatingBar)findViewById(R.id.ratingbar_Indicator);10        final RatingBar ratingBar_default = (RatingBar)findViewById(R.id.ratingbar_default);11 12        ratingBar_default.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){13 14    public void onRatingChanged(RatingBar ratingBar, float rating,15      boolean fromUser) {16     ratingBar_Small.setRating(rating);17     ratingBar_Indicator.setRating(rating);18     Toast.makeText(AndroidRatingBar.this, "rating:"+String.valueOf(rating),19       Toast.LENGTH_LONG).show();20    }});21    }22 }

XML檔案

 1 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3    android:orientation="vertical" 4    android:layout_width="fill_parent" 5    android:layout_height="fill_parent" 6    > 7 <TextView  8    android:layout_width="fill_parent" 9    android:layout_height="wrap_content"10    android:text="@string/hello"11    />12 <RatingBar 13    android:layout_width="wrap_content"14    android:layout_height="wrap_content"15    style="?android:attr/ratingBarStyleIndicator"16    android:id="@+id/ratingbar_Indicator"17    />18 <RatingBar 19    android:layout_width="wrap_content"20    android:layout_height="wrap_content"21    style="?android:attr/ratingBarStyleSmall"22    android:id="@+id/ratingbar_Small"23    android:numStars="20"24    />25 <RatingBar 26    android:layout_width="wrap_content"27    android:layout_height="wrap_content"28    style="?android:attr/ratingBarStyle"29    android:id="@+id/ratingbar_default"30    />31 </LinearLayout>

 

結束

  本文為"madgoat"和"wallace2010"聯合署名,原因是翻譯重了,不過兩個翻譯得都很好,這樣翻譯重的事件也將隨著管理的完善得意解決,感謝兩位的相互理解,感謝大家的支援!

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.