Android怎樣控制評價小星星的顯示(能獲取到星星的個數)
阿新 • • 發佈:2018-12-31
程式碼中: mRatingBar.setRating(3.8f);
程式碼:
package com.example.sj.xiaoxingxing2;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.RatingBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private Context context;
private RatingBar mRatingBar ;
private TextView mRatingBarLevel ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// context = this;
mRatingBar = (RatingBar)findViewById(R.id.service_grade_ratingBar);//星
mRatingBarLevel = (TextView)findViewById(R.id.service_grade_level);//星個數
mRatingBar.setRating(3.8f);
// mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
// @Override
// public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
// float i = mRatingBar.getRating();
// mRatingBarLevel.setText(i + "");
// }
// });
}
}
佈局檔案:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sj.xiaoxingxing2.MainActivity">
<!-- 星級評分條 -->
<RatingBar
android:id="@+id/service_grade_ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:isIndicator="true"
android:numStars="5"
android:rating="1.5" />
<TextView
android:id="@+id/service_grade_level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>