Android 如何在多个活动之间保存int值
我有5个问题,每个问题都在不同的活动中,Raddiobuttongroup有3个可能的答案,但只有一个是真的每个问题都有20分,并且有一个下一个和上一个按钮从一个问题移动到另一个问题,当你完成5个答案时,你在结果活动中有你的结果,但只有当我没有使用任何上一个答案时,它才有效按钮我如何通过使用“上一个”按钮来实现这一点,并且可以在这里更改我的答案是代码Android 如何在多个活动之间保存int值,android,Android,我有5个问题,每个问题都在不同的活动中,Raddiobuttongroup有3个可能的答案,但只有一个是真的每个问题都有20分,并且有一个下一个和上一个按钮从一个问题移动到另一个问题,当你完成5个答案时,你在结果活动中有你的结果,但只有当我没有使用任何上一个答案时,它才有效按钮我如何通过使用“上一个”按钮来实现这一点,并且可以在这里更改我的答案是代码 public class science_first_activity extends AppCompatActivity {
public class science_first_activity extends AppCompatActivity {
int sum=0;
RadioButton r2;
RadioGroup g;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_first_activity);
r2=findViewById(R.id.radio_2);
g=findViewById(R.id.group1);
}
public void next(View v)
{
if(g.getCheckedRadioButtonId()==r2.getId()) {
sum = sum + 20;
Intent i = new Intent(this, science_second_activity.class);
i.putExtra("mark", sum);
startActivity(i);
}
}
在第二、第三、第四和第五次活动中
我写了这段代码
public class science_second_activity extends AppCompatActivity {
RadioGroup g;
RadioButton b;
int sum =0;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_second_activity);
g=findViewById(R.id.group1);
b=findViewById(R.id.radio_2);
t=findViewById(R.id.textView2);
Intent i = getIntent();
sum=i.getIntExtra("mark",0);
String s=String.valueOf(sum);
t.setText(s);
}
public void next(View view)
{
if(g.getCheckedRadioButtonId()==b.getId()) {
sum = sum + 20;
Intent i = new Intent(this,science_resault_activity.class);
}
Intent i = new Intent(this,science_third_activity.class);
i.putExtra("mark",sum);
startActivity(i);
}
public void privious(View v)
{
Intent i = new Intent(this,science_first_activity.class);
startActivity(i);
}
在我写的结果活动中
public class science_resault_activity extends AppCompatActivity {
TextView t;
int sum=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_science_resault_activity);
Intent i = getIntent();
t=findViewById(R.id.textView7);
sum=i.getIntExtra("mark",0);
String s = String.valueOf(sum);
t.setText(s);
}
解决方案1: 在您的第一个活动
onCreate
:
Intent i = new Intent(this, ActivityTwo.class);
String answer= "Your answer to pass";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“key”, answer);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
在第二个活动中,在oncreate中保留这样的内容
Bundle bundle = getIntent().getExtras();
//Extract the data…
String answer = bundle.getString(“key”);
检查
解决方案2:
或者您可以使用共享首选项
永久保存值像这样创建一个SharedReference
SharedPreferences sharedPref = getActivity().getSharedPreferences(
"answer_key", Context.MODE_PRIVATE);
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("answer_key", newValueToPutThereInText);
editor.apply();
单击按钮或以任何方式更改值时,将值写入其中
SharedPreferences sharedPref = getActivity().getSharedPreferences(
"answer_key", Context.MODE_PRIVATE);
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("answer_key", newValueToPutThereInText);
editor.apply();
这将把newValueToPutThereInText
永久保存到SharedReference
要访问该值,请使用
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int answer = sharedPref.getInt("answer_key", anyDefaultValueToUse);
Textview toUse = findViewById(R.id.your_id);
toUse.setText(answer);
记住在所有SharedReferences
对象中使用相同的键,这里answer\u键是您的键
看我认为这是一个很好的答案!两个伟大的解决方案,我还将提交更复杂的问题,您可以在多个活动或片段之间使用共享视图模型。看看这个