基本上,我用幾個EditTexts 制作了一個片段,用戶可以在其中插入有關自己的資料,例如體重、身高、年齡等。我還添加了一個TextView根據插入的資料顯示該人的基礎代謝率的片段,但是問題是,每當我更改此人的資訊時,它都不會自動更新。我假設這是因為計算是在onCreateView方法內部進行的,因此如果我靜態預插入相應的資料,它只會計算 BMR。那么,我該如何做到這一點,每當我更改某個變數(fe age)的值時,它也會自動重新計算該人的 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>
uj5u.com熱心網友回復:
您可以將 TextWatcher 用于您的 EditTexts。當用戶在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) {
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/424655.html
