Android Scoring Control Ratingbar use instance resolution _android

Source: Internet
Author: User

No matter the game, the application, the website, the scoring controls are indispensable. The Ratingbar control is provided in the Android SDK to achieve the appropriate work.

<RatingBar/> Tags have several common scoring related attributes

Android:numstars, specify the score of the number of pentagram.
Android:rating, specifying the current score
Android:stepsize, specifying fractional increments

<RatingBar/> There are 3 common style properties

The default style is Ratingbarstyle
Style ratingbarstyleindicator not interactive, medium size
Style Ratingbarstylesmall not interactive, little Star

Nonsense not much to say that directly on the code

Activity_main.xml Layout file

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <!--default style is Ratingbarstyle, and the second is the same--> <!--default parameters: Star 5 Step 0.5 initial value 0--> <ratingbar android:id= "@ +id/ratingbar1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "/> <!--style RatingB Arstyle--> <!--parameters: Star 4 Step 0.5 initial value 1--> <ratingbar android:id= "@+id/ratingbar2"? style= Ngbarstyle "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:numStars=" 4 "Android: Stepsize= "0.5" android:rating= "1"/> <!--style ratingbarstyleindicator not interactive, medium size--> <!--parameters: Star 5 initial value 2 not Interaction, set step size meaningless--> <ratingbar android:id= "@+id/ratingbarone" style= "Android:attr/ratingbarstyleindicator" Androi D:layout_width= "Wrap_content" Android: layout_height= "Wrap_content" android:numstars= "5" android:rating= "2"/> <!--style Ratingbarstylesmall not interactive, small star Star--> <!--parameters: Star 4 Initial value 2 is not interactive, set step size meaningless--> <ratingbar android:id= "@+id/ratingbartwo" style= "android:attr/r"?
  Atingbarstylesmall "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:numStars=" "4"
 android:rating= "2"/> </LinearLayout>

Here we can clearly see the various properties of the Ratingbar, but what about the Ratingbar (the first two) for interaction and how to use it to show the Ratingbar (the last two) that cannot interact?

Here we implement a click on the interactive RatingBar1 above to get the score and update it to the Ratingbarone for display
Click on the interactive RatingBar2 above to get the score and update it to the Ratingbartwo for display.

The key method is to set up the listening Setonratingbarchangelistener and set the scoring method for the scoring component setrating (float rating)

Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.RatingBar;

Import Android.widget.RatingBar.OnRatingBarChangeListener;
 public class Mainactivity extends activity {//Interoperable two ratingbar private ratingbar ratingBar1 = null;

 Private Ratingbar ratingBar2 = null;
 Non-interactive two ratingbar private ratingbar ratingbarone = null;

 Private Ratingbar ratingbartwo = null;
  protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
  Set Layout page Setcontentview (r.layout.activity_main);
 initialization function Initview (); 
  The///initialization function public void Initview () {//Findviewbyid respectively gets control RATINGBAR1 = (Ratingbar) Findviewbyid (R.ID.RATINGBAR1);
  RatingBar2 = (Ratingbar) Findviewbyid (R.ID.RATINGBAR2);
  Ratingbarone = (Ratingbar) Findviewbyid (R.id.ratingbarone);

  Ratingbartwo = (Ratingbar) Findviewbyid (r.id.ratingbartwo); Add Onratingbarchangelistener for RATINGBAR1///when user interacts to change score, triggers the event Ratingbar1.setonratingbarchangelistener (new OnratingbarchAngelistener () {//This method can get to 3 parameters public void onratingchanged (Ratingbar ratingbar, float rating, Boolean
      Paramboolean) {////first parameter current rating modified by Ratingbar System.out.println (Ratingbar);
      The second parameter current score score, Range 0~ star number System.out.println (rating);

      The third parameter Returns True System.out.println (Paramboolean) If the score change is triggered by the user touching the gesture or the direction key trajectory movement; The RATINGBAR1 score parameter of the setrating//set to Onratingchanged method is obtained by the scoring score of the Ratingbarone display type rating
     (rating);

  }
    }); Add Onratingbarchangelistener for RATINGBAR2///when user interacts to change score, triggers the event Ratingbar2.setonratingbarchangelistener (new Onratingbarchangelistener () {public void onratingchanged (Ratingbar ratingbar, float rating, Boolean Parambool EAN) {////The RATINGBAR2 score parameter of the setrating//set to the Onratingchanged method is ratingbartwo the score score of the non interactive display type rating
     Two.setrating (rating);
 }
    });
 }
}

When the code runs in the activity, the first two ratingbar of the click operation are successful in implementing the two ratingbar after the update?
The value of the 3 parameters of the Setonratingbarchangelistener can be clearly seen on the SYSTEM.OUT.PRINTLN console output.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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.