我无法理解共享偏好是如何工作的,所以我需要你的帮助。
如何存储计数器的数据,以便当用户杀死/暂停/进入应用程序中的新活动然后返回此活动时,计数器在离开时仍处于相同状态?
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
counter =counter+(n1*n2);
TextView totalemission;
totalemission=(TextView)findViewById(R.id.total);
totalemission.setText(String.valueOf(counter));
另外,我希望用户能够在返回应用程序时继续向计数器添加(n1 * n2)。
这是代码的其余部分:
package com.carschoolingpromo.scires.carschooling;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class Calculator extends AppCompatActivity {
public static final String MY_PREFS_NAME ="MyPrefsFile";
int counter =0;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
Button caremissionscheck = findViewById(R.id.checkcar);
caremissionscheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent tocarbonemissions = new Intent(Calculator.this, CarbonEmissions.class);
startActivity(tocarbonemissions);
}
});
final Button distancecheck = findViewById(R.id.checkdestination);
distancecheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent checkdistance = new Intent(Calculator.this, DistancesList.class);
startActivity(checkdistance);
}
});
final Button multiply =(Button)findViewById(R.id.multiplycounter);
final EditText num1 =(EditText)findViewById(R.id.carboninput);
final EditText num2 =(EditText)findViewById(R.id.distanceinput);
final TextView ans =(TextView)findViewById(R.id.answer);
final Button gotonext=(Button)findViewById(R.id.nextsequence);
multiply.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
ans.setText(String.valueOf(n1*n2));
Button gettotal;
gettotal=(Button)findViewById(R.id.carpooltoday);
gettotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String number1 = num1.getText().toString();
String number2 = num2.getText().toString();
final int n1 = Integer.parseInt(num1.getText().toString());
final int n2 = Integer.parseInt(num2.getText().toString());
counter =counter+(n1*n2);
final TextView totalemission;
totalemission=(TextView)findViewById(R.id.total);
totalemission.setText(String.valueOf(counter));
final String string = totalemission.getText().toString();
Button save, load;
save =(Button)findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onResume();
onStart();
onStop();
sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("number1", num1.getText().toString());
editor.putString("number2", num2.getText().toString());
editor.putString("emission",totalemission.getText().toString());
editor.apply();
Toast.makeText(Calculator.this, "Saved!", Toast.LENGTH_SHORT).show();
}
});
load=(Button)findViewById(R.id.load);
load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String firstnum = sharedPreferences.getString("number1", "");
String secondnum = sharedPreferences.getString("number2", "");
String thirdnum = sharedPreferences.getString("emission", "");
TextView carcarbon, distance, totalexp;
carcarbon=(TextView)findViewById(R.id.car1);
carcarbon.setText(firstnum);
distance=(TextView)findViewById(R.id.distance1);
distance.setText(secondnum);
totalexp=(TextView)findViewById(R.id.previoustotal);
totalexp.setText(thirdnum);
}
});
final String carbonfootprint = ans.getText().toString();
gotonext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent k = new Intent(Calculator.this, NextStep.class);
k.putExtra("carbonfootprint", carbonfootprint);
startActivity(k);
}
});
}
});
}
});}}
答案 0 :(得分:1)
我看到你刚刚导入SharedPreferences,请做一些研究 Android Shared preferences example和https://developer.android.com/training/basics/data-storage/shared-preferences.html
在偏好设置中设置值:
// MY_PREFS_NAME - a static String variable like:
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("n1", Integer.parseInt(num1.getText().toString()));
editor.putInt("n2", Integer.parseInt(num2.getText().toString()));
editor.apply();
当用户返回应用程序时,从首选项中检索数据:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int n1 = prefs.getInt("n1", 0);// 0 is the default value.
int n2 = prefs.getInt("n2", 0); //0 is the default value.