Android学习之RatingBar评分组件

main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/MyLayout"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<RatingBar
		android:numStars="5"
		android:stepSize="0.5"
		android:isIndicator="false"
		android:id="@+id/ratingbarA"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<RatingBar
		android:numStars="5"
		android:rating="3"
		android:isIndicator="true"
		android:id="@+id/ratingbarB"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"/>
	<TextView
		android:id="@+id/text"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>
</LinearLayout>

 

 

MyRatingBarDemo.java:

 

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

public class MyRatingBarDemo extends Activity {
	private RatingBar ratingBarA = null; 					// 定义评分组件
	private TextView text = null; 							// 文本显示组件

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.ratingBarA = (RatingBar) super.findViewById(R.id.ratingbarA) ;
		this.text = (TextView) super.findViewById(R.id.text) ;// 取得组件
		this.ratingBarA.setOnRatingBarChangeListener(
				new OnRatingBarChangeListenerImpl());		// 设置监听
	}

	private class OnRatingBarChangeListenerImpl implements
			RatingBar.OnRatingBarChangeListener {
		@Override
		public void onRatingChanged(RatingBar ratingBar, float rating,
				boolean fromUser) {
			MyRatingBarDemo.this.text.append("*** 当前值(Rating):"
					+ ratingBar.getRating() + ",增长步长:"
					+ ratingBar.getStepSize() + "\n"); 		// 增加文本显示
		}
	} 
}

 

 

 

 

 

你可能感兴趣的:(RatingBar,评分组件)