Java 如何在android中保存值?
我是创建android应用程序的新手 我尝试使用在线指南创建一个温度转换应用程序。 () 我想知道是否有可能存储我之前在步骤18.8中作为输入输入输入的值,然后在应用程序的另一个页面中以表格格式显示这些值 如果可能的话,您能指导我如何存储这些值吗 多谢各位 (我将澄清是否需要任何其他信息。) 编辑: 我打算将温度值(键入)写入文本文件 以下代码来自该网站 主课Java 如何在android中保存值?,java,android,Java,Android,我是创建android应用程序的新手 我尝试使用在线指南创建一个温度转换应用程序。 () 我想知道是否有可能存储我之前在步骤18.8中作为输入输入输入的值,然后在应用程序的另一个页面中以表格格式显示这些值 如果可能的话,您能指导我如何存储这些值吗 多谢各位 (我将澄清是否需要任何其他信息。) 编辑: 我打算将温度值(键入)写入文本文件 以下代码来自该网站 主课 package com.vogella.android.temperatureconverter; import android.ap
package com.vogella.android.temperatureconverter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.inputValue);
}
// this method is called at button click because we assigned the name to the
// "OnClick" property of the button
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
if (text.getText().length() == 0) {
Toast.makeText(this, "Please enter a valid number",
Toast.LENGTH_LONG).show();
return;
}
float inputValue = Float.parseFloat(text.getText().toString());
if (celsiusButton.isChecked()) {
text.setText(String
.valueOf(ConverterUtil.convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String
.valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
break;
}
}
}
实用类
package com.vogella.android.temperatureconverter;
public class ConverterUtil {
// converts to celsius
public static float convertFahrenheitToCelsius(float fahrenheit) {
return ((fahrenheit - 32) * 5 / 9);
}
// converts to fahrenheit
public static float convertCelsiusToFahrenheit(float celsius) {
return ((celsius * 9) / 5) + 32;
}
}
public void writedata(String data) {
//BufferedWriter out = null;
try{
FileOutputStream out = new FileOutputStream(new File("outputfile.txt"));
out.write(ConverterUtil.convertFahrenheitToCelsius(inputValue.getBytes());
out.close();
}
}
(我已经修改了网站上的实用程序代码,加入了write file方法。)
但是我的程序不起作用。我不确定写入文本文件的代码是否错误。
另外,我不知道在哪里可以访问文本文件
我是写文本文件的新手。希望你能引导我度过难关。谢谢 您可以使用Intent将数据从一个活动发送到另一个活动
Intent otherActivityIntent = new Intent(this,OtherActivity.class);
otherActivityIntent.putExtra("key","tempratureValue");
startActivity(otherActivityIntent);
在其他活动中:
在onCreate()中
要永久存储值,您可以将其存储在数据库支持的sqlite数据库中,也可以使用inputstream将其写入设备内存,或者您可以将其保存在共享首选项中,但在您的情况下,您只希望用户传递输入而不保存它,以便将浮点值设置为用于调用类似这样的活动:
intent.putExtra("tag",value)
然后在下一个活动中,通过调用
getIntent().getFloatExtra("tag", float defaultValue);
其中“tag”=变量的键,默认值为“tag”键没有结果时使用的值。如果将每个输入存储在SQL数据库中,则可以。有几种方法可以实现这一点。理想情况下,您需要尝试一些,如果您有问题,请回来。@durgadevi1发布您迄今为止尝试过的内容以获得更多帮助。您可以查找SharedReferences、Sqlite、Eventbus或intent extras。有很多可能的状态。我不知道哪个最适合你。请尝试带着更具体的问题和代码来到这里。我需要将值写入文本文件。谢谢你的回复谢谢你的回复
getIntent().getFloatExtra("tag", float defaultValue);