Android Star scoring control Ratingbar use

Source: Internet
Author: User

in theAndroiddevelopment, there is a called scoring control Ratingbar, we can use the control to do grading, scoring and other functions, star shape Display, can also be half-star level, we look at how the scoring control is used.

The layout file defines the controls as well as the properties, where the main requirement is to specify the total number of stars, and the current value, that is, the total level with the current level of the amount.

<ratingbar
Android:id= "@+id/ratingbar"
android:numstars= "5"//general level, total score, number of stars
android:rating= "1.5"//current level, score, number of stars
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
</RatingBar>

Two of the more important methods in scoring controls are:

Ratingbar.setrating (Flaot rating);
Ratingbar.getrating ();

Event Listener Processing:

Ratingbar.setonratingbarchangelistener (Newonratingbarchangelistener () {
@Override
Public voidonratingchanged (Ratingbar Ratingbar, Floatrating, Boolean fromuser) {
Doing actions
}
});

This article originates from http://1622511.blog.51cto.com/1612511/567316

Android Chinese Api--ratingbar

Body

First, the structure

    Publicclass 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 the Seekbar and ProgressBar, using a star to show the grading. When using the default size of Ratingbar, the user can touch/drag or use the keys to set the score, it has two styles (small style with ratingbarstylesmall, large style with Ratingbarstyleindicator), where the big one is only suitable for instruction, Not suitable for user interaction.

When using a ratingbar that can support user interaction, it is inappropriate to place the control (widgets) on its left or right.

The number of stars (defined by the function setnumstars (int) or in the layout file of the XML) is displayed only when the width of the layout is set to wrap content (the consequences are unpredictable if set to another layout width).

The secondary progress should not be modified, as he is merely a fill background within the star-shaped part.

See Formstuff Tutorial.

Three, nested class

Interface: Ratingbar.onratingbarchangelistener

A callback function that modifies the client's star rating when the star progress changes.

Iv. XML attributes

Property name

Describe

Android:isindicator

Ratingbar is an indicator (user cannot make changes)

Android:numstars

The number of stars displayed must be an integer value, like "100".

Android:rating

The default rating must be a floating-point type, like "1.2".

Android:stepsize

The step size of the rating must be a floating-point type, like "1.2".

V. Public methods

public int Getnumstars ()

Returns the number of stars displayed

return value

Number of stars displayed

Public Ratingbar.onratingbarchangelistenergetonratingbarchangelistener ()

return value

Listener (may be empty) monitor score Change Event

public float getrating ()

Gets the current score (number of stars populated)

return value

Current ratings

public float getstepsize ()

Get the step of the scoring bar

return value

Step

public boolean isindicator ()

return value

Determine if the current scoring bar is just an indicator (note: can be modified)

public void Setisindicator (Booleanisindicator)

Sets whether the current scoring bar is just an indicator (so the user cannot modify the operation)

Parameters

isindicator bool value, whether it is an indicator

Public synchronized void Setmax (Intmax)

Set the range of rating levels from 0 to Max

Parameters

Max scoring bar maximum range.

public void Setnumstars (Intnumstars)

Sets the number of star types displayed. To be able to display them normally, it is recommended that the current widget's layout width be set to

Wrap Content

Parameters

Numstars number of star types

public void Setonratingbarchangelistener (Ratingbar.onratingbarchangelistener listener)

Sets the listener for callbacks when the rating level changes

Parameters

Listener Listener

public void setrating (floatrating)

Set the score (number of stars)

Parameters

Rating set the score

public void Setstepsize (floatstepsize)

Set the step size of the current scoring bar (step sizes)

Parameters

Stepsize the step in the scoring bar. For example: If you want half a star, it has a value of 0.5.


Vi. protected methods

protectedsynchronized void Onmeasure (intwidthmeasurespec, int heightmeasurespec)

Weigh the view and content to determine its width and height neatly. It is called by measure (Int,int) and should be covered by the quilt class to provide accurate and efficient layout measurements.

Rule: When overriding this method, you must call Setmeasureddimension (Int,int) to store the exact width and height of the view. If you do not do this, the llegalstateexception exception is triggered and thrown by the function measure (int,int). It is reasonable to call the parent class Onmeasure (Int,int).

The implementation of the basic class of dimensions defaults to the background size unless the large size is allowed through MEASURESPEC. Subclasses should overwrite onmeasure (int, int) to provide a better layout size.

If this method is overridden, the subclass should be responsible for ensuring that the standard width and height are at least the values of the minimum widths and heights of the View (Getsuggestedminimumheight () and Getsuggestedminimumwidth () two methods respectively).

Parameters

The Widthmeasurespec is subject to the horizontal space required by the main window. This requirement is encoded by VIEW.MEASURESPEC.

HeightmeasurespecThe vertical space requirement dictated by the main window. This requirement is encoded by VIEW.MEASURESPEC.

Vii. Supplementary

Article links

Android Control Ratingbar scoring bar

Android Replacement Ratingbar Pictures

[Android Learning Guide] Ratingbar rating Strips

Sample code (code reproduced from Android mobile Developer forum)

Java files

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 file

<?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>

End

This article is "Madgoat" and "wallace2010" joint signature, because the translation is heavy, but two translations are very good, so the translation of heavy events will be satisfied with the management of the consummation of the solution, thanks to two mutual understanding, thank you for your support!

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

      • Previous Android scoring bar Ratingbar custom settings
      • Next Android Background selector selector Usage Summary

Android Star scoring control Ratingbar use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.