gpt4 book ai didi

java - 使用共享首选项在 EditText 中保存数字

转载 作者:行者123 更新时间:2023-12-01 17:16:08 24 4
gpt4 key购买 nike

我是 Android 编程新手,我创建了一个简单的计算器。我希望用户能够暂停应用程序,而不会丢失在两个 EditText 框中输入的数字。我正在尝试使用共享首选项,但由于某种原因我的应用程序不断崩溃。我该如何解决这个问题?

package com.example.simplecalculator;

import android.os.Bundle;


import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.simplecalculator.*;


public class MainActivity extends Activity {

SharedPreferences mySharedPreferences;
SharedPreferences.Editor editor;
public static final String MYPREFERENCES = "MyPreferences_001";
float answer;
final EditText numberone = (EditText)findViewById(R.id.number1);
final EditText numbertwo = (EditText)findViewById(R.id.number2);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mySharedPreferences = getSharedPreferences(MYPREFERENCES, 0);
String number1 = mySharedPreferences.getString("number1", null);
String number2 = mySharedPreferences.getString("number2", null);
numberone.setText(number1);
numbertwo.setText(number2);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void Addition(View view) {

answer = Float.parseFloat(numberone.getText().toString()) + Float.parseFloat(numbertwo.getText().toString());;

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your sum is " + answer);


}

public void Subtraction(View view) {

answer = Float.parseFloat(numberone.getText().toString()) - Float.parseFloat(numbertwo.getText().toString());;

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your difference is " + answer);

}


public void Multiplication(View view) {
answer = Float.parseFloat(numberone.getText().toString()) * Float.parseFloat(numbertwo.getText().toString());;

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your product is " + answer);

}

public void Division(View view) {

answer = Float.parseFloat(numberone.getText().toString()) / Float.parseFloat(numbertwo.getText().toString());;

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your quotient is " + answer);

}

public void Power(View view) {
answer = (float) Math.pow(Float.parseFloat(numberone.getText().toString()), Float.parseFloat(numbertwo.getText().toString()));

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your answer is " + answer);

}

public void Root(View view) {

answer = (float) Math.sqrt(Float.parseFloat(numberone.getText().toString()));

TextView value = (TextView)findViewById(R.id.answer);
value.setText("Your answer is " + answer);

}


public void onPause(){

mySharedPreferences = getSharedPreferences(MYPREFERENCES, Activity.MODE_PRIVATE);
editor = mySharedPreferences.edit();
editor.putString("number1", numberone.getText().toString());
editor.putString("number2", numbertwo.getText().toString());
editor.commit();

}

}

最佳答案

final EditText numberone = (EditText)findViewById(R.id.number1);
final EditText numbertwo = (EditText)findViewById(R.id.number2);

将其保留在 onCreate() 内,在 setContentView() 之后

此外,当sharedPref中没有存储任何内容时,第一次会发生什么,

String number1 = mySharedPreferences.getString("number1", null);
String number2 = mySharedPreferences.getString("number2", null);

然后返回一些值而不是“null”。

如果在计算器应用程序中经常使用 SharedPref,我建议创建一个用于获取和放置 SharedPref 的函数..
类似于:

public static void saveDataToPreferences(String key,
String value) {
SharedPreferences prefs = context.getSharedPreferences("your package name",
Context.MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static String getDataFromPreferences(String key) {

SharedPreferences prefs = context.getSharedPreferences("your package name",
Context.MODE_PRIVATE);
return prefs.getString(key, Constants.BLANK);
}

如果函数位于您的 Activity 内部,则可以全局声明该 Activity 的上下文。否则将其传递到参数中。

关于java - 使用共享首选项在 EditText 中保存数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22031810/

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