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