Android RatingBar 评分条

RatingBar评分条是SeekBar拖动条的子类。现阶段系统自带了3种样式,我们同样用例子来演示他的使用方法和属性设置:
1、main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<RatingBar android:id="@+id/RatingBar01"
		android:layout_width="wrap_content" android:rating="3"
		android:layout_marginTop="20dp" android:layout_height="wrap_content">
	</RatingBar>
	<RatingBar android:id="@+id/RatingBar03"
		android:layout_width="wrap_content" android:numStars="5"
		android:rating="4" android:isIndicator="false"
		style="?android:attr/ratingBarStyleIndicator"
		android:layout_marginTop="20dp" android:layout_height="wrap_content">
	</RatingBar>
	<RatingBar android:id="@+id/RatingBar02"
		android:layout_width="wrap_content" android:isIndicator="false"
		android:numStars="10" android:rating="8"
		style="?android:attr/ratingBarStyleSmall" android:layout_marginTop="20dp"
		android:layout_height="wrap_content">
	</RatingBar>
</LinearLayout> 



2、在HelloRatingBar.java的代码里我们实现了3个评分条联动的功能:

package com.esri.arcgis.sample;

import android.app.Activity;

import android.os.Bundle;

import android.widget.RatingBar;

import android.widget.RatingBar.OnRatingBarChangeListener;

public class AndroidRatingBar extends Activity {

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 定义组件对象
		final RatingBar rb1 = (RatingBar) findViewById(R.id.RatingBar01);
		final RatingBar rb2 = (RatingBar) findViewById(R.id.RatingBar02);
		final RatingBar rb3 = (RatingBar) findViewById(R.id.RatingBar03);

		// 定义评分监听器
		OnRatingBarChangeListener orbcl = new OnRatingBarChangeListener() {

			@Override
			public void onRatingChanged(RatingBar ratingBar, float rating,

			boolean fromUser) {
				switch (ratingBar.getId()) {
				case R.id.RatingBar01:
					// 把第一个评分条的值取出来设置给其他评分条
					rb2.setRating(rb1.getRating());
					rb3.setRating(rb1.getRating() * 2);// 十颗星所以乘以2
					break;
				case R.id.RatingBar02:
					rb1.setRating(rb2.getRating());
					rb3.setRating(rb2.getRating() * 2);
					break;
				case R.id.RatingBar03:
					rb1.setRating(rb3.getRating() / 2);
					rb2.setRating(rb3.getRating() / 2);
					break;
				}
			}
		};

		// 绑定监听器
		rb1.setOnRatingBarChangeListener(orbcl);
		rb2.setOnRatingBarChangeListener(orbcl);
		rb3.setOnRatingBarChangeListener(orbcl);
	}
}


3、运行程序,点击评分条看看效果,使用左右键再试试

Android RatingBar 评分条
有兴趣的可以研究一下,如何更换评分条中的图片,譬如罢星星换成花朵什么的。
好,本讲就到这里。

你可能感兴趣的:(android,xml,OS)