Regardless of games, applications, or websites, rating controls are indispensable. The RatingBar control is provided in the Android SDK to achieve corresponding work.
<RatingBar/> tagThere are several commonly used scoring related attributes
android:numStars, specify the number of rated five-pointed stars.
android:rating, specify the current score
android:stepSize, specify the fraction increment
<RatingBar/>There are 3 more commonly used style attributes
The default style is ratingBarStyle
style ratingBarStyleIndicator not interactive, medium size
style ratingBarStyleSmall not interact, little star
Without further ado, just upload the code
activity_main.xml layout file
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- defaultstyle that isratingBarStyle,Same as the second one --> <!-- defaultparameter:star5 Step length0.5 Initial value 0 --> <RatingBar android: android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- style ratingBarStyle --> <!-- parameter:star4 Step length0.5 Initial value 1 --> <RatingBar android: style="?android:attr/ratingBarStyle" 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 --> <!-- parameter:star5 Initial value 2 Not interactive,设置Step length无意义 --> <RatingBar android: style="?android:attr/ratingBarStyleIndicator" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="2"/> <!-- style ratingBarStyleSmall Not interactive,小starstar --> <!-- parameter:star4 Initial value 2 Not interactive,设置Step length无意义 --> <RatingBar android: style="?android:attr/ratingBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="4" android:rating="2" /> </LinearLayout>
Running here we can clearly see the various properties of RatingBar, but how should we use RatingBar (first two) for interaction and RatingBar (second two) for displaying the incomprehensible RatingBar (second two)?
Here we implement a click on the RatingBar1 above, get the rating, and update to the RatingBarOne for display
Click on the interactive RatingBar2 above to get the rating and update to the RatingBarTwo for display.
The key method is to set the listening setOnRatingBarChangeListener and set the scoring method for the scoring component to setRating (float rating)
import ; import ; import ; import ; public class MainActivity extends Activity { // Two interactive ratingBars private RatingBar ratingBar1 = null; private RatingBar ratingBar2 = null; // Two ratingBars that are not interactive private RatingBar ratingBarOne = null; private RatingBar ratingBarTwo = null; protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); // Set the layout page setContentView(.activity_main); // Initialize the function initView(); } // Initialize the function public void initView() { // Obtain controls separately through findViewById ratingBar1 = (RatingBar) findViewById(.ratingBar1); ratingBar2 = (RatingBar) findViewById(.ratingBar2); ratingBarOne = (RatingBar) findViewById(); ratingBarTwo = (RatingBar) findViewById(); // Add OnRatingBarChangeListener to ratingBar1 // This event is triggered when the user interaction changes the score (new OnRatingBarChangeListener() { // This method can obtain 3 parameters public void onRatingChanged(RatingBar ratingBar, float rating, boolean paramBoolean) { // The first parameter ratingBar modified by the current score (ratingBar); // The second parameter: Current score, range: 0~number of stars (rating); // Third parameter Return true if the score change is triggered by the user's touch gesture or the direction key trackball movement (paramBoolean); //Change the score of the non-interactive display ratingBarOne through setRating //Set the score parameter ratingBar1 obtained by the onRatingChanged method to ratingBar1 rating (rating); } }); // Add OnRatingBarChangeListener to ratingBar2 // This event is triggered when the user interaction changes the score (new OnRatingBarChangeListener() { public void onRatingChanged(RatingBar ratingBar, float rating, boolean paramBoolean) { //Change the score of the non-interactive display ratingBarTwo through setRating //Set the score parameter ratingBar2 obtained by the onRatingChanged method to ratingBar2 rating (rating); } }); } }
After writing the code in the Activity, did the first two RatingBars successfully implement the last two RatingBars?
The values of the setOnRatingBarChangeListener can be clearly seen on the console output.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.