gpt4 book ai didi

Android:关闭应用程序时保存实例状态

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:12 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How do I save an Android application's state?

我是 Java 和 Android 的新手,正在构建一些小项目来学习。我制作了一个金钱跟踪应用程序,它允许用户输入值,它只是继续减去它。一切正常,但我希望在关闭并重新打开应用程序时保存或缓存这些值。阅读后,我发现 OnPause 或许可以解决问题,但仍未 100% 理解它。

谁能推荐如何执行此操作以及如何应用到我的代码中?

非常感谢您的帮助!

package ps.age.sl;


import java.text.NumberFormat;
import java.util.Locale;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;


public class MoneyTrackerActivity extends Activity {
/** Called when the activity is first created. */
ImageButton subtract;


EditText startingmoney,submoney, endmoney, tracker;
Locale currentLocale = Locale.getDefault();





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

// startingmoney = (EditText) findViewById (R.id.firstmoney);
// submoney = (EditText) findViewById (R.id.submoney);
// subtract = (ImageButton) findViewById (R.id.subbutton);
// endmoney = (EditText) findViewById (R.id.endtv);
// tracker = (EditText) findViewById (R.id.trackertv);

startingmoney.setText("");
submoney.setText("");
endmoney.setText("");

subtract.setOnClickListener(new View.OnClickListener() {
double currentValue=0;
double startValue=0;
public void onClick(View v) throws NumberFormatException {


if (v == subtract)
{
NumberFormat currencyFormatter;
currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);



String totalString;
String x = startingmoney.getText().toString();
String y = submoney.getText().toString ();
double total;
double xm = 0.00;
double ym =0.00;

try
{
xm = Double.parseDouble(x);
}
catch(NumberFormatException n)
{
xm = 0.00;
}
try
{
ym = Double.parseDouble(y);
}
catch(NumberFormatException n)
{
ym = 0.00;
}

if(startValue!=xm){
startValue=xm;
currentValue=xm;
}

currentValue = currentValue -ym;


totalString = currencyFormatter.format(currentValue);
endmoney.setText(totalString);

tracker.setText("you have entered " + totalString +"\n" + tracker.getText().toString());



}
}
});
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com