如何在 Android Studio 中更改参数时自动更新数据

How to automatically update data when parameters are altered in Android Studio

基本上,我制作了一个带有几个 EditText 的片段,用户可以在其中插入有关他自己的数据,例如体重、身高、年龄等。我还添加了一个 TextView,其中根据插入的数据显示那个人的基础代谢率,但问题是,每当我更改这个人的信息时,它不会自动更新。 我假设这是因为计算是在 onCreateView 方法内部进行的,因此如果我静态预插入相应的数据,它只会计算 BMR。 那么,我怎样才能做到,每当我更改某个变量(f.e 年龄)的值时,它也会自动重新计算该人的 BMR 值?

JAVA

public class home_fragment extends Fragment {

   TextView genderSelector;
   TextView activitySelector;
   TextView goalSelector;
   TextView metabolicRate;
   EditText eHeight; String height;
   EditText eWeight; String weight;
   EditText eAge; String age;




   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {

       // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.fragment_home_fragment, container, false);

      


       activitySelector = (TextView) view.findViewById(R.id.tvActivitySelector);
       genderSelector = (TextView) view.findViewById(R.id.tvGenderSelector);
       goalSelector = (TextView) view.findViewById(R.id.tvGoalSelector);
       metabolicRate = (TextView) view.findViewById(R.id.tvBasalMetabolicRateCalculator);
       
       eHeight = (EditText) view.findViewById(R.id.etHeight);
       eWeight = (EditText) view.findViewById(R.id.etWeight);
       eAge = (EditText) view.findViewById(R.id.etAge);

       eHeight.setText("181");
       eWeight.setText("80");
       eAge.setText("17");
       genderSelector.setText("Female");

       height = eHeight.getText().toString();
       weight = eWeight.getText().toString();
       age = eAge.getText().toString();


       if (!height.equals(null) && !weight.equals(null) && !age.equals(null)) {
           double h = Double.parseDouble(height);
           double w = Double.parseDouble(weight);
           double a = Double.parseDouble(age);

           metabolicRate.setText(Double.toString(calculate(h, w, a, genderSelector)));

       }

       activitySelector.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent i = new Intent(getActivity(), ActivityLevel.class);
               startActivity(i);
           }
       });

       goalSelector.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Intent i = new Intent(getActivity(), Goal.class);
               startActivity(i);
           }
       });

       return view;
   }

   private double calculate(double h, double w, double a, TextView g) {
       if (g.getText().toString().equals("Male")) {
           return 5 + (10 * w) + (6.25 * h) - (5 * a);
       } else {
           return -161 + (10 * w) + (6.25 * h) - (5 * a);
       }
   }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".profile_fragment"
    android:background="@color/lightgrey"
    android:id="@+id/profile_frag">


    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/ProfileInformation"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@color/grey">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Profile"
                android:textColor="@color/textGray"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="13dp"
                android:textSize="19dp"
                android:layout_marginBottom="6dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/HeightProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/ProfileInformation"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvHeight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Height (cm)"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

            <EditText
                android:id="@+id/etHeight"
                android:layout_width="wrap_content"
                android:layout_height="18dp"
                android:layout_centerVertical="true"
                android:hint="cm"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:textColorHint="@color/textGray"
                android:textSize="18dp"
                android:cursorVisible="true"
                android:background="@color/lightblack"
                android:textCursorDrawable="@null"
                android:layout_marginRight="10dp"
                android:gravity="right"
                />

        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/WeightProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/HeightProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvWeight"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Weight (kg)"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

            <EditText
                android:id="@+id/etWeight"
                android:layout_width="wrap_content"
                android:layout_height="18dp"
                android:layout_centerVertical="true"
                android:hint="kg"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:textColorHint="@color/textGray"
                android:textSize="18dp"
                android:cursorVisible="true"
                android:background="@color/lightblack"
                android:textCursorDrawable="@null"
                android:layout_marginRight="10dp"
                android:gravity="right"
                />

        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/GenderProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/WeightProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvGender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Gender"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>


            <TextView
                android:id="@+id/tvGenderSelector"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Male"
                android:textSize="18dp"
                android:textColor="@color/white"
                android:layout_marginRight="10dp"
                android:layout_alignParentRight="true"

                />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/AgeProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/GenderProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="60dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvAge"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Age"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

            <EditText
                android:id="@+id/etAge"
                android:layout_width="wrap_content"
                android:layout_height="18dp"
                android:layout_centerVertical="true"
                android:hint="years"
                android:textColor="@color/white"
                android:layout_alignParentRight="true"
                android:textColorHint="@color/textGray"
                android:textSize="18dp"
                android:cursorVisible="true"
                android:background="@color/lightblack"
                android:textCursorDrawable="@null"
                android:layout_marginRight="10dp"
                android:gravity="right"
                />

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ActivityProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/AgeProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="90dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvActivity"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Activity"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

            <TextView
                android:id="@+id/tvActivitySelector"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Moderate"
                android:layout_alignParentRight="true"
                android:textColor="@color/white"
                android:layout_centerVertical="true"
                android:textSize="18dp"
                android:layout_marginRight="10dp">

            </TextView>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/GoalProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/ActivityProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvGoal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Goal"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />



            </RelativeLayout>

            <TextView
                android:id="@+id/tvGoalSelector"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Lose weight"
                android:layout_alignParentRight="true"
                android:textColor="@color/white"
                android:layout_centerVertical="true"
                android:textSize="18dp"
                android:layout_marginRight="10dp">

            </TextView>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/CustomizeMealsProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/GoalProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="150dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Customize meals"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/ResultInformation"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_below="@id/CustomizeMealsProfile"
            android:background="@color/grey">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Results"
                android:textColor="@color/textGray"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="13dp"
                android:textSize="19dp"
                android:layout_marginBottom="6dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/BasalMetabolicRateProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/ResultInformation"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="190dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvBasalMetabolicRate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Basal Metabolic Rate"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />


            </RelativeLayout>

            <TextView
                android:id="@+id/tvBasalMetabolicRateCalculator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="kcal"
                android:textColor="@color/white"
                android:layout_centerVertical="true"
                android:textSize="18dp"
                android:layout_marginRight="10dp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/BodyMassIndexProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/BasalMetabolicRateProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="190dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvBodyMassIndex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Body Mass Index"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />

            </RelativeLayout>

            </RelativeLayout>

        <RelativeLayout
            android:id="@+id/WaterRequirementProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/BodyMassIndexProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="210dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvWaterRequirement"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Water Requirement (ml)"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />

            </RelativeLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/DailyCaloricRequirementProfile"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_below="@id/WaterRequirementProfile"
            android:background="@color/lightblack"
            android:layout_marginTop="1dp"
            >

            <RelativeLayout
                android:layout_width="230dp"
                android:layout_height="match_parent"
                android:elevation="1dp"
                android:background="@color/lightblack"
                >

                <TextView
                    android:id="@+id/tvCaloricRequirement"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:text="Daily Caloric Requirement"
                    android:textColor="@color/textGray"
                    android:layout_marginLeft="13dp"

                    />

            </RelativeLayout>

        </RelativeLayout>

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

您可以为 EditText 使用 TextWatcher。 当用户在 editText 中更改文本然后重新计算时

    yourEditText.addTextChangedListener(new TextWatcher() {

   @Override
   public void afterTextChanged(Editable s) {}

   @Override    
   public void beforeTextChanged(CharSequence s, int start,
     int count, int after) {
   }

   @Override    
   public void onTextChanged(CharSequence s, int start,
     int before, int count) {
      
   }
  });