gpt4 book ai didi

java - android SharedPreferences 将整数传递给下一个 Activity

转载 作者:行者123 更新时间:2023-11-29 20:51:27 24 4
gpt4 key购买 nike

<分区>

我如何将整数值从 TextView 传递到下一个 Activity ?目前我使用字符串作为我的共享首选项,每次我将它更改为 int 我的应用程序强制关闭。

这是我在 mainactivity 上的代码

int scoreText=50;
public static final String PREFS_COIN= "MyPreferenceFile";

protected void onCreate(Bundle savedInstanceState) {

public void checkAnswer()
{ String answer=answerText.getText().toString();

if(isCorrect(answer))
{

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("What are you a GENIUS?!");
builder.setMessage("Nice one, Genius! You have P10!");
builder.setIcon(android.R.drawable.btn_star);
builder.setPositiveButton("View Trivia",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
scoreText+=10;
scoreNew=scoreText;
scoreTxt.setText(""+ scoreNew);
SharedPreferences settings2=getSharedPreferences(PREFS_COIN, 0);
SharedPreferences.Editor editor2=settings2.edit();
editor2.putString("coins", scoreTxt.getText().toString());
editor2.commit();
Intent intent=new Intent(getApplicationContext(), Luzon1Trivia.class);
startActivity(intent);
overridePendingTransition(R.animator.transition_fade_in, R.animator.transition_fade_out);
//startActivity(new Intent(Luzon1.this, Luzon2.class));

;} });
AlertDialog alert = builder.create();
alert.show(); // Show Alert Dialog
scoreTxt.setVisibility(View.GONE);

//disable all the buttons and textview
answerText.setEnabled(false);
answerButton.setClickable(false);
}
}

用户每次猜对答案,都会获得+10的硬币。问题是,在第二个 Activity 中,我无法添加/减去 sharedpreference 值,因为它被声明为字符串。发生的事情是 TextView 中出现“60 +10”

这是我在 Activity 2 中的代码

public static final String PREFS_COIN= "MyPreferenceFile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.luzon2);

scoreTxt = (TextView)findViewById(R.id.score);
SharedPreferences settings2=getSharedPreferences(PREFS_COIN, 0);
scoreTxt.setText(settings2.getString("coins", ""));

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