Ich arbeite an einem Projekt mit Fragmenten, wo ich 4 Bewertungsbalken habe (mit jeweils vier Sternen) und der Benutzer kann nur 10 verbrauchen "Sterne" kombiniert aus allen Bewertungsbalken. Ich versuche gerade, die Werte zu addieren, aber selbst das funktioniert nicht. Ich möchte in der Lage sein, alle Werte zusammenzufassen und dann zu beschränken, wie viele Werte in allen Bewertungen festgelegt werden können.Erhalten Sie den Wert von mehreren Bewertungsbalken und setzen Sie eine Grenze für die Anzahl der Sterne
FragmentRatings.java:
`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:
<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>
Kein Android-Entwickler, aber es scheint kein OnChanging-Event zu geben, um vorher/nachher zu erfassen. Also würde ich wahrscheinlich "aktuelle" Werte für jede Ihrer Statistiken erstellen. Wenn ich beispielsweise die Stärke ändere, würde ich currentWisdom + currentIntlect + currentDexterity + rbStrength.getRating() nehmen und wenn es größer als 10 ist, entweder: a) rbStrength.setRating (currentStrength) // reset oder b) rbStrength.setRating (10 - currentWisdom - currentIntlect - currentDexterity) // max verbleibend kann ich es setzen. Wenn es <= 10 ist, currentStrength = rbStrength.getRating() – Robert