Android 之RatingBar

上篇介绍了SeekBar(可托动进度条)的基本用法,今天介绍RatingBar,类似星星评分,先看下RagingBar的主要属性

 

  • numStars 星星的数量
  • progress 当前等级
  • stepSize 每前进一格代表步划

 下面通过一个简单的示例演示RatingBar的使用,

<RatingBar 
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.5"
        />
	<Button 
	    android:id="@+id/buttn1" 
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_below="@id/ratingBar1"
	    android:text="评分"
	     />

 numStars:星星显示的个数

stepSize:没前进的步划

package com.zhoucj.ratingbardemo;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;

public class MainActivity extends Activity
{

    RatingBar ratingBar;
    Button button1;
    public static String TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ratingBar=(RatingBar)findViewById(R.id.ratingBar1);
        ratingBar.setOnRatingBarChangeListener(new RatingBarChangeListener());
        button1=(Button)findViewById(R.id.buttn1);
        button1.setOnClickListener(new ClickListener());
        
    }

    class RatingBarChangeListener implements OnRatingBarChangeListener
    {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating,
                boolean fromUser)
        {
            Log.i(TAG, "当前分数="+rating);
        }
    }
    
    class ClickListener implements OnClickListener
    {
        @Override
        public void onClick(View v)
        {    //在当前加0.5分
            ratingBar.setRating(ratingBar.getRating()+0.5f);
        }
    }
    
    
    @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;
    }

}

上面演示了OnRatingBarChangeListener监听的使用,点击评分按钮时,显示RatingBar的分数。

posted @ 2013-06-17 15:45  最後的輕語  阅读(280)  评论(0编辑  收藏  举报