本文主要是介绍RatingBar使用小例子,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图如下:
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"><TextViewandroid: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"/><TextViewandroid: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 {@Overridepublic 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));}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_rating_bar, menu);return true;}}
这篇关于RatingBar使用小例子的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!