RatingBar使用完整示例

时间:2022-03-20 20:48:45

MainActivity如下:

package cc.testratingbar;

import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.app.Activity;
/**
 * Demo描述:
 * RatingBar使用完整示例
 * 如果要自定义RatingBar更改它的外观,只需要在布局文件中修改其style即可
 * 
 * 参考资料:
 * 1 http://www.cnblogs.com/over140/archive/2010/11/18/1880455.html
 * 2 http://www.cnblogs.com/lipeil/archive/2012/07/13/2590457.html
 * 3 http://my.oschina.net/amigos/blog/60060
 *   Thank you very much
 *
 */
public class MainActivity extends Activity {
    private RatingBar mRatingBar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
	private void init(){
		mRatingBar=(RatingBar) findViewById(R.id.ratingBar);
		//为RatingBar设置监听
		mRatingBar.setOnRatingBarChangeListener(new RatingBarChangeListenerImpl());
		//得到RatingBar的最大等级
		int max=mRatingBar.getMax();
		//得到RatingBar现在的等级
		float currentRating=mRatingBar.getRating();
		System.out.println("max="+max+",currentRating"+currentRating);
	}
	
	//注意onRatingChanged方法中的最后一个参数boolean fromUser:
	//若是由用户触摸手势或方向键轨迹球移动触发RatingBar的等级改变,返回true
	//若是由编程触发RatingBar的等级改变,返回false
	private class RatingBarChangeListenerImpl implements OnRatingBarChangeListener{
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
			
			System.out.println("现在的等级为 rating="+rating+",是否是用户触发 fromUser="+fromUser);
		}
		
	}

	
}

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="RatingBar使用完整示例" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:max="5"
        android:rating="2" />

</RelativeLayout>