RatingBar是基于SeekBarProgressBar的扩展,用星型来显示等级评定。使用RatingBar的默认大小时,用户可以触摸/拖动或使用键来设置评分,它有两种样式(小风格用ratingBarStyleSmall,大风格用ratingBarStyleIndicator)。在工程中设置了三种样式风格的RatingBar,并且当点击按钮的时候可以改变星星的级数。

效果图:

 

android之RatingBar_android 

layout文件中的xml文件:

 

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical"   
  5.     android:background="#ff00ff"> 
  6.  
  7.     <TextView 
  8.         android:id="@+id/myTextView" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="wrap_content" 
  11.         android:text="当前评分:" /> 
  12.     <RatingBar   
  13.         android:id="@+id/myRatingBar1" 
  14.         android:numStars="5" //设置星星的数量
  15.         android:rating="5"  //设置当前的星星级数
  16.         android:layout_width="wrap_content" 
  17.         android:layout_height="wrap_content" 
  18.         android:isIndicator="false" 
  19.         style="?android:attr/ratingBarStyle" //设置RatingBar的样式
  20.         /> 
  21.       
  22.     <RatingBar   
  23.         android:id="@+id/myRatingBar2" 
  24.         android:numStars="5" 
  25.         android:rating="5" 
  26.         android:layout_width="wrap_content" 
  27.         android:layout_height="wrap_content" 
  28.         android:isIndicator="false" 
  29.         style="?android:attr/ratingBarStyleIndicator" 
  30.         /> 
  31.       
  32.     <RatingBar   
  33.         android:id="@+id/myRatingBar3" 
  34.         android:numStars="5" 
  35.         android:rating="5" 
  36.         android:layout_width="wrap_content" 
  37.         android:layout_height="wrap_content" 
  38.         android:isIndicator="false" 
  39.         style="?android:attr/ratingBarStyleSmall" 
  40.         /> 
  41.  
  42. </LinearLayout> 

Activaty.java文件

 

  1. package com.cheng.ratingbarproject;  
  2.  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.RatingBar;  
  7. import android.widget.TextView;  
  8. import android.widget.RatingBar.OnRatingBarChangeListener;  
  9.  
  10. public class RatingBarProject extends Activity {  
  11.     //定义组件  
  12.     private RatingBar mRatingBar1;  
  13.     private RatingBar mRatingBar2;  
  14.     private RatingBar mRatingBar3;  
  15.     private TextView mTextView;  
  16.     @Override 
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         super.setContentView(R.layout.main);  
  20.         //获得组件  
  21.         mRatingBar1 = (RatingBar)findViewById(R.id.myRatingBar1);  
  22.         mRatingBar2 = (RatingBar)findViewById(R.id.myRatingBar2);  
  23.         mRatingBar3 = (RatingBar)findViewById(R.id.myRatingBar3);  
  24.         mTextView = (TextView)findViewById(R.id.myTextView);  
  25.           
  26.         //设置RatingBar的监听器  
  27.         OnRatingBarChangeListener orbcl = new OnRatingBarChangeListener() {  
  28.             //当点击的时候将当前的星星级数改变为点击的位置的值  
  29.             @Override 
  30.             public void onRatingChanged(RatingBar ratingBar, float rating,  
  31.                     boolean fromUser) {  
  32.                 // TODO Auto-generated method stub  
  33.                 mRatingBar2.setRating(rating);  
  34.                 mRatingBar3.setRating(rating);  
  35.                 mTextView.setText("当前分数:"+ rating);  
  36.                   
  37.             }  
  38.         };  
  39.         //绑定监听器  
  40.         mRatingBar1.setOnRatingBarChangeListener(orbcl);  
  41.     }  
  42.  
  43.     @Override 
  44.     public boolean onCreateOptionsMenu(Menu menu) {  
  45.         // Inflate the menu; this adds items to the action bar if it is present.  
  46.         getMenuInflater().inflate(R.menu.main, menu);  
  47.         return true;  
  48.     }  
  49.  
  50. }  

ok,今天到此了。