BMI-Rechner

  • 1 Antworten
  • Letztes Antwortdatum
B

Beka1801

Neues Mitglied
0
Hallo,

ich habe versucht einen BMI-Rechner zu erstellen. Allerdings beim Click auf den Button wird die App beendet. Hier mal meinen Code.
Im "Home_Fragment"

private EditText groesse;
private EditText gewicht;
private TextView ergebnis;

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);

//final TextView textView = root.findViewById(R.id.text_home);
//homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
// @override
// public void onChanged(@Nullable String s) {
// textView.setText(s);
return root;

}
// });
//return root;


public void calculateBMI(View v) {
String heightStr = groesse.getText().toString();
String weightStr = gewicht.getText().toString();


if (heightStr != null && !"".equals(heightStr)
&& weightStr != null && !"".equals(weightStr)) {
float heightValue = Float.parseFloat(heightStr) / 100;
float weightValue = Float.parseFloat(weightStr);

float bmi = weightValue / (heightValue * heightValue);

displayBMI(bmi);
}
}

private void displayBMI(float bmi) {
String bmiLabel = "";

if (Float.compare(bmi, 15f) <= 0) bmiLabel = getString(R.string.very_severely_underweight);
else if (Float.compare(bmi, 15f) > 0 && Float.compare(bmi, 16f) <= 0) {
bmiLabel = getString(R.string.severely_underweight);
} else if (Float.compare(bmi, 16f) > 0 && Float.compare(bmi, 18.5f) <= 0) {
bmiLabel = getString(R.string.underweight);
} else if (Float.compare(bmi, 18.5f) > 0 && Float.compare(bmi, 25f) <= 0) {
bmiLabel = getString(R.string.normal);
} else if (Float.compare(bmi, 25f) > 0 && Float.compare(bmi, 30f) <= 0) {
bmiLabel = getString(R.string.overweight);
} else if (Float.compare(bmi, 30f) > 0 && Float.compare(bmi, 35f) <= 0) {
bmiLabel = getString(R.string.obese_class_i);
} else if (Float.compare(bmi, 35f) > 0 && Float.compare(bmi, 40f) <= 0) {
bmiLabel = getString(R.string.obese_class_ii);
} else {
bmiLabel = getString(R.string.obese_class_iii);
}

bmiLabel = bmi + "\n\n" + bmiLabel;
ergebnis.setText(bmiLabel);

XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">

<EditText
android:id="@+id/etGewicht"
android:layout_width="127dp"
android:layout_height="42dp"
android:ems="10"
android:hint="Gewicht in kg"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.134"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.208" />

<EditText
android:id="@+id/etGroesse"
android:layout_width="109dp"
android:layout_height="43dp"
android:ems="10"
android:hint="Größe in cm"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.125"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.095" />

<Button
android:id="@+id/btBerechnen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="calculateBMI"
android:text="Berechnen"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.117"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.329" />

<TextView
android:id="@+id/tvAnzeigen"
android:layout_width="408dp"
android:layout_height="71dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.665" />
</androidx.constraintlayout.widget.ConstraintLayout>
 
A

Anzeige

  • Gerade eben
  • Neu
Hallo Beka1801,

schau mal hier: BMI-Rechner. Dort wird jeder fündig!
Hallo das onklickt tack im XML layout kannst du bei Fragmenten nicht benutzen.
Du musst in der onCreateview mit der view einen listner erstellen.
 
Zurück
Oben Unten