RatingBar for android control development
RatingBar for android control developmentThis blog post mainly describes the development of RatingBar. This control is mainly used for scoring and rating. Let's take a look at the instance code below:
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 );
// Bind the listener of the RatingBar
RatingBar. setOnRatingBarChangeListener (new setRatingBarListener ());
}
// Create a listener for RatingBar
Class setRatingBarListener implements RatingBar. OnRatingBarChangeListener {
// Call this method when the RatingBar Level 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. xml: 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">
Android: id = "@ + id/myText"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"/>
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"/>
The display effect is as follows:
You can change the increment by setting stepSize.