The Ratingbar of Android control development

Source: Internet
Author: User

The Ratingbar of Android control development This blog is mainly about the development of the Ratingbar, this control is mainly used for scoring, ratings, let's look at the example code:
Mainactivity.java: Package Com.example.ratingbar;


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


public class Mainactivity extends Activity {


Private Ratingbar Ratingbar = null;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Ratingbar = (Ratingbar) Findviewbyid (R.id.myratingbar);
Ratingbar-bound listener
Ratingbar.setonratingbarchangelistener (New Setratingbarlistener ());
}


Creating a Ratingbar Listener
Class Setratingbarlistener implements ratingbar.onratingbarchangelistener{



This method is called when the Ratingbar hierarchy changes
@Override
public void onratingchanged (Ratingbar ratingbar, float rating,
Boolean Fromuser) {
TODO auto-generated Method Stub
System.out.println ("Ratingbar--->" + rating);
}

}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


}


Main layout file Main.xml: <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">


<textview
Android:id= "@+id/mytext"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>

<!--numstars How many stars stepsize indicate how many stars to move each time
In the width setting, it is best to use wrap_content, otherwise you can only set the score in Numstars, and cannot set the number of stars displayed on the screen
-
<ratingbar
Android:id= "@+id/myratingbar"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@+id/mytext"
Android:numstars= "4"
android:stepsize= "0.5"/>

</RelativeLayout>


The display results are as follows:

You can change the amplitude of each increment by setting the stepsize



The Ratingbar of Android control development

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.