android应用程序使用按钮保存和加载值

时间:2015-09-19 12:11:56

标签: java android

我编写了一个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
    }

}

2 个答案:

答案 0 :(得分:1)

您可以使用Android内置的偏好系统。

请查看本指南:http://developer.android.com/training/basics/data-storage/shared-preferences.html

基本上,您可以访问以下首选项:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

您存储的值如下:

SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit();

您可以像这样检索值:

int defaultValue = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

答案 1 :(得分:1)

您可以使用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));

}