我已经对BMI计算器进行了编程,现在我想用一个按钮保存和加载体重和身高值,有人知道怎么做吗?

package com.example.test.bmicalculator;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;

import android.widget.EditText;
import android.widget.TextView;

public class MyActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }


    public void calculateClickHandler(View view) {
        // make sure we handle the click of the calculator button

        if (view.getId() == R.id.calculateButton) {

            // get the references to the widgets
            EditText weightText = (EditText)findViewById(R.id.weightText);
            EditText heightText = (EditText)findViewById(R.id.heightText);
            TextView resultText = (TextView)findViewById(R.id.resultLabel);

            // get the users values from the widget references

            float weight = Float.parseFloat(weightText.getText().toString());
            float height = Float.parseFloat(heightText.getText().toString());

            // calculate the bmi value

            float bmiValue = calculateBMI(weight, height);

            // interpret the meaning of the bmi value
            String bmiInterpretation = interpretBMI(bmiValue);

            // now set the value in the result text

            resultText.setText(bmiValue + "-" + bmiInterpretation);

        }


    }

    // the formula to calculate the BMI index

    // check for http://en.wikipedia.org/wiki/Body_mass_index
    private float calculateBMI (float weight, float height) {

        return (float) (weight * 4.88 / (height * height));
    }


    // interpret what BMI means
    private String interpretBMI(float bmiValue) {

        if (bmiValue < 16) {
            return "Severely underweight";
        } else if (bmiValue < 18.5) {

            return "Underweight";
        } else if (bmiValue < 25) {

            return "Normal";
        } else if (bmiValue < 30) {

            return "Overweight";
        } else {
            return "Obese";
        }

    }




    public void SaveClickHandler(View view) {

        // here I want to Save the weight and the height value
    }


    public void LoadClickHandler(View view) {
        // here I want to Load the weight and the height value
    }

}

最佳答案

您可以使用SharedPreferences进行以下操作:

public void SaveClickHandler(View view) {

    SharedPreferences prefs = getSharedPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putFloat("height", height);
    editor.putFloat("weight", weight);
    editor.commit();
}


当您想要获得此值时,请执行以下操作:

public void LoadClickHandler(View view) {

    float height = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    float weight= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    //Do more stuff
}


编辑

将这些heightweight设置为全局变量,如下所示:

浮子的高度,重量;

然后在onCreate()中执行以下操作:

weight = Float.parseFloat(weightText.getText().toString());
height = Float.parseFloat(heightText.getText().toString());


并将此代码添加到您的LoadClickHandler()

public void LoadClickHandler(View view) {

    float heightSaved = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    float weightSaved= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    //Do more stuff
}


改变这个:

SharedPreferences prefs = getSharedPreferences(Context.MODE_PRIVATE);


对此:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());


最终密码

public class MyActivity extends Activity {
float weight, height;
SharedPreferences prefs;
EditText weightText, heightText;
TextView resultText;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
     prefs = PreferenceManager.getDefaultSharedPreferences(this);
    // get the references to the widgets
    weightText = (EditText)findViewById(R.id.weightText);
    heightText = (EditText)findViewById(R.id.heightText);
    resultText = (TextView)findViewById(R.id.resultLabel);
}

public void calculateClickHandler(View view) {
    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.calculateButton) {
        // get the users values from the widget references
        weight = Float.parseFloat(weightText.getText().toString());
        height = Float.parseFloat(heightText.getText().toString());

        // calculate the bmi value

        float bmiValue = calculateBMI(weight, height);

        // interpret the meaning of the bmi value
        String bmiInterpretation = interpretBMI(bmiValue);

        // now set the value in the result text

        resultText.setText(bmiValue + "-" + bmiInterpretation);

    }


}

// the formula to calculate the BMI index

// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI (float weight, float height) {

    return (float) (weight * 4.88 / (height * height));
}


// interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Severely underweight";
    } else if (bmiValue < 18.5) {

        return "Underweight";
    } else if (bmiValue < 25) {

        return "Normal";
    } else if (bmiValue < 30) {

        return "Overweight";
    } else {
        return "Obese";
    }

}


public void SaveClickHandler(View view) {

    // here I want to Save the weight and the height value
    prefs = PreferenceManager.getDefaultSharedPreferences(view.getContext());
    SharedPreferences.Editor editor = prefs.edit();
    editor.putFloat("height", height);
    editor.putFloat("weight", weight);
    editor.apply();
}

public void LoadClickHandler(View view) {
    // here I want to Load the weight and the height value
    height = prefs.getFloat("height", 0.0f);  //Default value (0.0f)
    weight= prefs.getFloat("weight", 0.0f);   //Default value (0.0f)
    heightText.setText(String.valueOf(height));
    weightText.setText(String.valueOf(weight));

}

关于java - Android应用程序使用按钮保存和加载值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32668068/

10-09 08:10