RatingBar使用小例子

来源:互联网 发布:漆黑的追踪者知乎 编辑:程序博客网 时间:2024/05/06 20:21

效果图如下:

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() {@Overridepublic 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;    }    }