效果圖如下:
activity_rating_bar.xml中的代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".RatingBarActivity"
android:textColor="#ffffff"/>
<RatingBar
android:id="@+id/bigstar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyle"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textColor="#ffffff"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/good"
android:textColor="#ffffff"/>
<RatingBar
android:id="@+id/smallstar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>
RatingBarActivity.java中的代碼如下:
package com.bzu.ratingbar.activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.support.v4.app.NavUtils;
public class RatingBarActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating_bar);
final TextView result=(TextView) this.findViewById(R.id.result);
final RatingBar bigStar=(RatingBar) this.findViewById(R.id.bigstar);
final RatingBar smallStar=(RatingBar) this.findViewById(R.id.smallstar);
//點擊第一行評分
bigStar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
bigStar.setRating(rating);
smallStar.setRating(rating/2);
//顯示評分結果
result.setText(String.valueOf(rating));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_rating_bar, menu);
return true;
}
}