Android惯用控件之RatingBar的使用

Android常用控件之RatingBar的使用

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

Android惯用控件之RatingBar的使用

在布局文件中声明

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    

    <RatingBar 
        android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:numStars="5"
        android:stepSize="0.1"/>
</LinearLayout>
numStars表示有多少颗星,stepSize表示每次滑动的大小


在Activity中的代码

package com.example.ratingbar;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RatingBar;

import com.example.widgetdemo.R;

public class ratingBarActivity extends Activity {
	
	private RatingBar ratingBar = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ratingbar);
		ratingBar = (RatingBar)findViewById(R.id.ratingbar);
		ratingBar.setOnRatingBarChangeListener(new ratingChangeListener());
	}
	 class ratingChangeListener implements RatingBar.OnRatingBarChangeListener{

		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			// TODO Auto-generated method stub
			if(fromUser){
				System.out.println("onRatingChanged " + rating);
			}
		}
		 
	 }
}

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

点击打开链接