繁体   English   中英

如果值在小数位,android会设置评级栏中的部分星标

[英]android set part of the stars in the rating bar if the value is in decimal places

我想使用评级栏显示用户的分数。 例如,如果用户得到30个问题中的7个正确,则textview(tv_percentage)正确显示23.33%,但评级明星应该显示5星中的1.17星,但现在无论得分是多少,总是显示5颗星。

以下我做错了什么?

码:

    SharedPreferences score = this.getSharedPreferences("MyApp", 0);
    int userscore = score.getInt("score", 0);
    int userQnumber = score.getInt("number_of_question_to_selected", 9999);     

    double scoring100 = Double.valueOf(userscore) / Double.valueOf(userQnumber) *100;
    String stripped2 = Double.valueOf(scoring100).toString();       
    DecimalFormat myFormatter1 = new DecimalFormat("###,###,###.##");       

    stripped2 = myFormatter1.format(Double.valueOf(stripped2));     
    tv_percentage.setText(""+stripped2+"%");        

    ratingBar1.setRating(Float.parseFloat(stripped2)); 

编辑代码:

    float d= (float) (scoring100 /100 * 5);
    String S = Double.valueOf(d).toString();    
    tv2_90.setText(""+S); // for testing only, tv2_90 showing proper value
    ratingBar1.setStepSize((d));
    ratingBar1.setRating(Float.parseFloat(stripped2)); 

布局:

            <RatingBar
                android:id="@+id/ratingBar1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:isIndicator="true"
                android:max="100"
                android:numStars="5"
                android:stepSize="0.1" />

你可以在这里找到它:

    float d= (float) ((number*5) /100);
    RatingBar rb = (RatingBar) findViewById(R.id.ratingBar1);
    rb.setRating(d);

在你的布局上:

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:isIndicator="true"
    android:max="5"
    android:stepSize="0.01" />

如果要以精确点显示评级,则需要设置stepSize

rate.setStepSize((float) 0.1);
rate.setRating((float)3.6);

这将显示第四星被填补超过一半。

通过XML

<RatingBar
 .....
 android:max="5"
 android:numStars="5"
 android:progressTint="@color/golden"
 android:rating="2"
 android:stepSize="1"/>

通过Java代码

  ratingBar.setMax(5);
  ratingBar.setNumStars(5);
  ratingBar.setStepSize(0.1f);

等等......还有更多

您必须将RatingBar width属性设置为wrap_content否则上述属性将不会对UI产生影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM