0

我是 android 新手,我在我的应用程序中使用 RatingBar,现在我想根据 RatingBar 中的星星选择来计算评分数。我用过:num_of_rating = rating.getRating(); 在我的应用程序中,但我无法计算它。如果有人知道答案,请给我解决方案。

4

3 回答 3

2

xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/ratingText" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <RatingBar android:id="@+id/rating" 
        android:isIndicator="false"
        android:stepSize="1.0" 
        android:numStars="5" 
        android:rating="0.0" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </RatingBar>

</LinearLayout>

活动

ratingText=(TextView)findViewById(R.id.ratingText);
        rating=(RatingBar)findViewById(R.id.rating);
        rating.setOnRatingBarChangeListener(this);

    }

    // implement abstract method onRatingChanged
    public void onRatingChanged(RatingBar ratingBar,float rating, boolean fromUser){
        ratingText.setText(""+this.rating.getRating()); 
    }
于 2013-02-08T10:30:28.800 回答
0
ratingBar = (RatingBar) findViewById(R.id.ratingBar);
btnSubmit = (Button) findViewById(R.id.btnSubmit);

//if click on me, then display the current rating value.
btnSubmit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        Toast.makeText(MyAndroidAppActivity.this,
            String.valueOf(ratingBar.getRating()),
                Toast.LENGTH_SHORT).show();

    }

});
于 2013-02-08T10:26:48.793 回答
0

使用以下代码

浮动 num_of_rating = 0.0f;

RatingBar ratingbar_rb = (RatingBar)findViewById(R.id.ratingbar_rd);

ratingbar_rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {

        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
             num_of_rating = rating;
        }});
于 2013-02-08T10:31:50.863 回答