首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >获取多个分级条值,并设置可以使用的星数限制

获取多个分级条值,并设置可以使用的星数限制
EN

Stack Overflow用户
提问于 2017-07-01 04:12:43
回答 2查看 579关注 0票数 -1

我正在做一个碎片项目,在那里我有4个评级条(每个4星),用户只能使用所有评级条中的10个“星”。

我现在正在尝试将这些值相加,但即使这样也不起作用。我希望能够将所有值相加,然后限制在所有评级中可以设置的值的数量。

FragmentRatings.java:

代码语言:javascript
复制
public class FramentRatings extends Fragment{
    RatingBar rbStrength, rbIntellect, rbWisdom, rbDexterity;
    TextView tvPointSpent;
    float pointsS, pointsI, pointsW, pointsD, total;

    private SharedPreferences prefs;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_ratings, container, false);

        rbStrength = (RatingBar)view.findViewById(R.id.strengthRating);
        rbIntellect = (RatingBar)view.findViewById(R.id.intellectRating);
        rbWisdom = (RatingBar)view.findViewById(R.id.wisdomRating);
        rbDexterity = (RatingBar)view.findViewById(R.id.dexterityRating);


        tvPointSpent = (TextView)view.findViewById(R.id.tvPointsSpend);

        rbStrength.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                tvPointSpent.setText(""+rbStrength.getRating());
                pointsS = rbStrength.getRating();
            }
        });

        rbIntellect.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //tvPointSpent.setText(""+rbIntellect.getRating());
                pointsI = rbIntellect.getRating();
            }
        });

        rbWisdom.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //tvPointSpent.setText(""+rbStrength.getRating());
                pointsW = rbWisdom.getRating();
            }
        });

        rbDexterity.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                //tvPointSpent.setText(""+rbStrength.getRating());
                pointsD = rbDexterity.getRating();
            }
        });

        total = pointsD + pointsI + pointsS + pointsW;

        tvPointSpent.setText(""+pointsS);

        return view;
    }
}

fragment_ratings.xml:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/tvStrength"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/strengthRating"
        android:layout_marginEnd="59dp"
        android:layout_marginRight="59dp"
        android:layout_toLeftOf="@+id/strengthRating"
        android:layout_toStartOf="@+id/strengthRating"
        android:text="Strength:"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvIntellect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvStrength"
        android:layout_alignStart="@+id/tvStrength"
        android:layout_alignTop="@+id/intellectRating"
        android:text="Intellect:"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvWisdom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvIntellect"
        android:layout_alignStart="@+id/tvIntellect"
        android:layout_alignTop="@+id/wisdomRating"
        android:text="Wisdom:"
        android:textSize="30sp" />

    <RatingBar
        android:id="@+id/strengthRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="141dp"
        android:layout_marginRight="141dp"
        android:layout_marginTop="61dp"
        android:numStars="4"
        android:rating="0"
        android:stepSize="1.0"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <RatingBar
        android:id="@+id/intellectRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/strengthRating"
        android:layout_alignStart="@+id/strengthRating"
        android:layout_below="@+id/strengthRating"
        android:layout_marginTop="58dp"
        android:numStars="4"
        android:rating="0"
        android:stepSize="1.0" />

    <RatingBar
        android:id="@+id/wisdomRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/intellectRating"
        android:layout_alignStart="@+id/intellectRating"
        android:layout_below="@+id/intellectRating"
        android:layout_marginTop="61dp"
        android:numStars="4"
        android:rating="0"
        android:stepSize="1.0" />

    <RatingBar
        android:id="@+id/dexterityRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/wisdomRating"
        android:layout_alignRight="@+id/wisdomRating"
        android:layout_below="@+id/wisdomRating"
        android:layout_marginTop="66dp"
        android:numStars="4"
        android:rating="0"
        android:stepSize="1.0" />

    <TextView
        android:id="@+id/tvDexterity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvIntellect"
        android:layout_alignStart="@+id/tvIntellect"
        android:layout_alignTop="@+id/dexterityRating"
        android:text="Dexterity:"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvPoints"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tvDexterity"
        android:layout_alignStart="@+id/tvDexterity"
        android:layout_below="@+id/dexterityRating"
        android:layout_marginTop="62dp"
        android:text="Points left to spend: "
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tvPointsSpend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvPoints"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_toEndOf="@+id/tvPoints"
        android:layout_toRightOf="@+id/tvPoints"
        android:text="10"
        android:textSize="30sp" />


</RelativeLayout>

Picture of the rating bars

EN

回答 2

Stack Overflow用户

发布于 2017-07-01 04:26:51

可能把逻辑搞错了,但这句话不应该:

tvPointSpent.setText(""+pointsS);

be:

TvPointSpent.setText(String.valueOf(总数));

票数 0
EN

Stack Overflow用户

发布于 2018-07-04 00:04:17

我可能来晚了,但是

我建议使用pointsS = ratingBar.getRating();pointsS = rating;作为rest的快速解决方案,因为函数onRatingChanged(...)中的这些参数将获取更改后的和最终的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44854187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档