android应用程序使用按钮保存和加载值(android application save and load value with button)

编程入门 行业动态 更新时间:2024-10-23 15:17:12
android应用程序使用按钮保存和加载值(android application save and load value with button) java

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

I have programmed a BMI Calculator and now I want to save and load the weight and height value with a button, does anyone know how to do this?

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 }

编辑

将这些height和weight作为全局变量,如下所示:

浮子高度,重量;

然后在你的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)); }

You can do it with SharedPreferences as follows :

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(); }

And when you want to get this value you do this :

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 }

Edit

Make those height and weight as a global variables as follows :

float height, weight;

Then in your onCreate() you do this :

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

And add this code in your 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 }

Change this :

SharedPreferences prefs = getSharedPreferences(Context.MODE_PRIVATE);

to this :

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

FINAL CODE

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)); }

更多推荐

本文发布于:2023-07-04 10:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023544.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   按钮   加载   android   button

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!