[英]RatingBar in a AlertDialog : How to change the number of stars programmatically?
[英]RatingBar: How to count no. of selected stars from RatingBar
我是Android的新手,我在我的应用程序中使用RatingBar,现在我想根据RatingBar中的星星选择来计算评级数。 我用过:num_of_rating = rating.getRating(); 在我的应用程序,但我无法得到它的计数。 如果有人知道答案,请给我解决方案。
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());
}
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();
}
});
使用以下代码
float 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;
}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.