Android常用控件之RatingBar的使用

RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar


在布局文件中声明

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.   
  8.     <RatingBar   
  9.         android:id="@+id/ratingbar"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:numStars="5"  
  13.         android:stepSize="0.1"/>  
  14. </LinearLayout>  
numStars 表示有多少颗星, stepSize 表示每次滑动的大小


在Activity中的代码

[java]  view plain copy
  1. package com.example.ratingbar;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RatingBar;  
  6.   
  7. import com.example.widgetdemo.R;  
  8.   
  9. public class ratingBarActivity extends Activity {  
  10.       
  11.     private RatingBar ratingBar = null;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         // TODO Auto-generated method stub  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.ratingbar);  
  17.         ratingBar = (RatingBar)findViewById(R.id.ratingbar);  
  18.         ratingBar.setOnRatingBarChangeListener(new ratingChangeListener());  
  19.     }  
  20.      class ratingChangeListener implements RatingBar.OnRatingBarChangeListener{  
  21.   
  22.         @Override  
  23.         public void onRatingChanged(RatingBar ratingBar, float rating,  
  24.                 boolean fromUser) {  
  25.             // TODO Auto-generated method stub  
  26.             if(fromUser){  
  27.                 System.out.println("onRatingChanged " + rating);  
  28.             }  
  29.         }  
  30.            
  31.      }  
  32. }  

这个控件比较简单,同样也可以参考源码

//----------------------------------------------自己----------------------------

使用注意:android:rating表示的是当前选中的星星数量。

还要注意使用wrap_content。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值