我制作了一个 Android Rating Bar 程序来接受用户评分,计算总评分并将其显示在指标评分栏中。
现在我想在我的 Rating Bar 程序中添加另外两个功能:
- 我想在 ListView 中使用我现有的评分栏
- 其次,希望在 MyRatings 等其他活动中显示高到低评分项目
我的 Java 代码:-
public class InteractiveRatingBarActivity extends Activity implements
OnRatingBarChangeListener {
RatingBar getRatingBar;
RatingBar setRatingBar;
TextView countText;
int count;
float curRate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewsById();
setRatingBar.setRating(curRate);
getRatingBar.setOnRatingBarChangeListener(this);
}
private void findViewsById() {
getRatingBar = (RatingBar) findViewById(R.id.getRating);
setRatingBar = (RatingBar) findViewById(R.id.setRating);
countText = (TextView) findViewById(R.id.countText);
}
public void onRatingChanged(RatingBar rateBar, float rating,
boolean fromUser) {
DecimalFormat decimalFormat = new DecimalFormat("#.#");
curRate = Float.valueOf(decimalFormat.format((curRate * count + rating)
/ ++count));
Toast.makeText(InteractiveRatingBarActivity.this,
"New Rating: " + curRate, Toast.LENGTH_SHORT).show();
setRatingBar.setRating(curRate);
countText.setText(count + " Ratings");
}
}
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RatingBar
android:id="@+id/getRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
<RatingBar
android:id="@+id/setRating"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/getRating"
android:isIndicator="true"
android:numStars="5"
android:stepSize="0.1" />
<TextView
android:id="@+id/countText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/getRating"
android:layout_toRightOf="@+id/setRating"
android:paddingLeft="10dp" />
</RelativeLayout>