传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
九环刀
今天我们学习如何利用Android平台“九环刀 ”RatingBar实现评分功能,像电子相册、网上图书、微博文章等都有类似的评分需求。下面给出该情景的案例:
一、案例技术要点
1.android.widget.RatingBar类提供评分功能,它扩展了SeekBar和ProgressBar。
2.为RatingBar对象设置OnRatingBarChangeListener,监听RatingBar的实时变化,并回调其onRatingChanged()方法。
二、案例代码陈列
工程包目录
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.lynn.ratingbar" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".RatingBarMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>strings.xml
<resources> <string name="app_name">Android评分RatingBar</string> <string name="rating">请您评分</string> </resources>main.xml
<?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/rating" android:textSize="20sp" /> <RatingBar android:id="@+id/ratingBar" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>RatingBarMainActivity.java
package cn.lynn.ratingbar; import android.app.Activity; import android.os.Bundle; import android.widget.RatingBar; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.Toast; /** * RatingBar扩展了SeekBar和ProgressBar,并且提供评分的功能 * RatingBar案例:请您评分 * @author lynnli1229 */ public class RatingBarMainActivity extends Activity { private RatingBar ratingBar; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ratingBar = (RatingBar) findViewById(R.id.ratingBar); ratingBar.setMax(100); // 设置最大刻度值为100 ratingBar.setProgress(10); // 设置当前刻度值为10 ratingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { @Override public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { ratingBar.getProgress(); Toast.makeText(RatingBarMainActivity.this, "当前刻度:" + ratingBar.getProgress() + ",当前评分:" + rating, Toast.LENGTH_LONG).show(); } }); } }三、案例效果展示