Preface
This chapter is about Android. widget. ratingbar, translated as "score bar", Android 2.2 R1, translated from "madgoat" and "wallace2010". You are welcome to visit their blogs: Workshop "madgoat" and "wallace2010 "! I look forward to you join the android Chinese translation group, contact my over140@gmail.com.
Statement
You are welcome to repost, but please keep the original source of the article :)
Blog Garden: http://www.cnblogs.com/
Android Chinese translation group: http://www.cnblogs.com/over140/
Body
I. Structure
Public class ratingbar extends absseekbar
Java. Lang. Object
Android. View. View
Android. widget. progressbar
Android. widget. absseekbar
Android. widget. ratingbar
Ii. Overview
Ratingbar is an extension based on seekbar and progressbar. It uses a star to display the rating. The default ratingbar size allows you to set ratingbar scoring by touching/dragging or using keys. It has two styles (ratingbarstylesmall for small styles and ratingbarstyleindicator for large styles ), the large ones are only suitable for instructions and not for user interaction.
When you use a ratingbar that supports user interaction, it is not appropriate to place the control (widgets) on the left or right.
Only when the layout width is set to wrap content, the number of stars is set (defined by the setnumstars (INT) function or in the XML layout file) it will be displayed (if it is set to another layout width, the consequences will be unpredictable ).
Generally, the secondary progress should not be modified because it is only used as the filling background of the star part.
See form stuff tutorial.
Iii. nesting
Interface: ratingbar. onratingbarchangelistener
A callback function that modifies the client's star level when the star level progress changes.
Iv. xml attributes
Attribute name |
Description |
Android: isindicator |
Whether the ratingbar is an indicator (the user cannot change it) |
Android: numstars |
The number of displayed stars, which must be an integer value, such as "100 ". |
Android: Rating |
The default score must be a floating point type, such as "1.2 ". |
Android: stepsize |
The scoring step size must be of the floating point type, such as "1.2 ". |
V. Public Methods
Public int getnumstars ()
Returns the number of stars displayed.
Return Value
Number of stars displayed
Public ratingbar. onratingbarchangelistener getonratingbarchangelistener ()
Return Value
Listener (may be empty) listens for scoring change events
Public float getrating ()
Get the current rating (number of filled stars)
Return Value
Current score
Public float getstepsize ()
Get the size of the score bar
Return Value
Step Size
Public Boolean isindicator ()
Return Value
Judge whether the current score bar is only a indicator (Note: whether it can be modified)
Public void setisindicator (Boolean isindicator)
Set whether the current score bar is just a indicator (so that the user cannot modify it)
Parameters
Isindicator bool value, whether it is a indicator
Public synchronized void setmax (INT max)
Set the rating range from 0 to Max.
Parameters
Max score bar maximum range.
Public void setnumstars (INT numstars)
Set the number of displayed stars. We recommend that you set the layout width of the current widget
Wrap content
Parameters
Numstars star quantity
Public void setonratingbarchangelistener (ratingbar. onratingbarchangelistener listener)
Set the listener for callback when the rating Level Changes
Parameters
Listener listener
Public void setrating (float rating)
Set the score (number of star nodes)
Parameters
Score set by rating
Public void setstepsize (float stepsize)
Set the step size of the current score bar (step size)
Parameters
Stepsize: step of the score bar. For example, if you want a half star, its value is 0.5.
Vi. Protected Methods
Protected synchronized void onmeasure (INT widthmeasurespec, int heightmeasurespec)
Weigh view and content to determine its width and height. It is called by measure (INT, INT) and should be covered by the quilt class to provide accurate and efficient layout measurement.
Note: When overwriting this method, you must call setmeasureddimension (INT, INT) to store the exact width and height of the view. Otherwise, llegalstateexception is triggered and thrown by the measure (INT, INT) function. It is reasonable to call the onmeasure (INT, INT) of the parent class.
The implementation of the basic class of dimension is the background size by default, unless a large size is allowed through measurespec. Subclass should overwrite onmeasure (INT, INT) to provide a better layout size.
If this method is overwritten, The subclass should be responsible for ensuring that the standard width and height are at least the value of the smallest width and height of the view (getsuggestedminimumheight () and getsuggestedminimumwidth () methods respectively ).
Parameters
WidthmeasurespecHorizontal space requirements dominated by the main window. This requirement is encoded by view. measurespec.
HeightmeasurespecRequired by the vertical space dominated by the main window. This requirement is encoded by view. measurespec.
VII. Supplement
Article Link
Ratingbar rating bar for Android Control
Change ratingbar for Android
[Android Learning Guide] ratingbar score bar
Sample Code (the code is reproduced from the Android mobile Developer Forum)
Java files
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 file
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>
End
This article is co-signed by "madgoat" and "wallace2010" because the translation is heavy, but both are very good, so that the translation heavy event will also be resolved with the improvement of management, thank you for your understanding and support!
Turn: http://www.cnblogs.com/over140/archive/2010/11/18/1880391.html