gpt4 book ai didi

android - 如何创建乐谱?

转载 作者:行者123 更新时间:2023-11-29 15:43:50 25 4
gpt4 key购买 nike

我正在做一个问答应用程序,我想在每次人们标记正确答案时递增一个整数。当我处理多个 Activity (每个问题一个 Activity )时,我将 Extra 传递给每个 Activity ,当我回答 1º 问题时游戏将关闭,我想知道如何解决这个问题。代码如下:`

@Override
public class QuestionOneActivity extends AppCompatActivity {

int score = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_one);

Button btn = (Button)findViewById(R.id.button);
final RadioButton rd1 = (RadioButton)findViewById(R.id.radioButton);
final RadioButton rd2 = (RadioButton)findViewById(R.id.radioButton2);
final RadioButton rd3 = (RadioButton)findViewById(R.id.radioButton3);
final RadioButton rd4 = (RadioButton)findViewById(R.id.radioButton4);
final RadioButton rd5 = (RadioButton)findViewById(R.id.radioButton5);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd2.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd3.isChecked()){
++score;
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd4.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd5.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
}

});


}
}

和问题二 Activity :

@Override
public class QuestionTwoActivity extends AppCompatActivity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_two);

Intent intent = getIntent();
int score = intent.getIntExtra("score", 0);


Button btn2 = (Button)findViewById(R.id.button2);
final RadioButton rd6 = (RadioButton)findViewById(R.id.radioButton6);
final RadioButton rd7 = (RadioButton)findViewById(R.id.radioButton7);
final RadioButton rd8 = (RadioButton)findViewById(R.id.radioButton8);
final RadioButton rd9 = (RadioButton)findViewById(R.id.radioButton9);
final RadioButton rd10 = (RadioButton)findViewById(R.id.radioButton10);

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd6.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd7.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd8.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd9.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
if(rd10.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionThreeActivity.class);
intent.putExtra("score", score);
startActivity(intent);
}
}

});


}
}

`问题是:它在分数变量的第二个问题 Activity 中显示错误消息。它说我必须使用最终声明,但我不能使用最终声明,因为如果有人正确回答了问题,我必须增加分数。我该如何解决这个问题?

最佳答案

我回答过类似的问题How to save score to SharedPreferences then update it .

这是怎么做的:

这就是您创建 SharedPreferences

的方式
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("score", YOUR_INT_VALUE);
editor.commit();

然后在需要获取该值的地方,您只需这样做:

sharedPreferences.getInt("score", 0)

希望你已经清楚了。

示例

您的 QuestionOneActivity 应该如下所示:

public class QuestionOneActivity extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
int score = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question_one);
//If you want you can initialite the score to 0 doing this (IT'S NOT NECESSARY)
SharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor = sharedPreferences.edit();
editor.putInt("score", 0);
editor.commit();
Button btn = (Button)findViewById(R.id.button);
final RadioButton rd1 = (RadioButton)findViewById(R.id.radioButton);
final RadioButton rd2 = (RadioButton)findViewById(R.id.radioButton2);
final RadioButton rd3 = (RadioButton)findViewById(R.id.radioButton3);
final RadioButton rd4 = (RadioButton)findViewById(R.id.radioButton4);
final RadioButton rd5 = (RadioButton)findViewById(R.id.radioButton5);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(rd1.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd2.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
//Everytime you want to ++ your score, now you'll do this
if(rd3.isChecked()){
editor = sharedPreferences.edit();
editor.putInt("score", (sharedPreferences.getInt("score", 0))+1);
editor.commit();
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd4.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
if(rd5.isChecked()){
Intent intent = new Intent(getApplicationContext(),QuestionTwoActivity.class);
startActivity(intent);
}
}
});
}

然后而不是做

int score = intent.getIntExtra("score", 0);

你可以这样做:

int score = sharedPreferences.getInt("score", 0);

注意:我不确定这条线是否有效

editor = sharedPreferences.edit();
editor.putInt("score", (sharedPreferences.getInt("score", 0))+1);
editor.commit();

如果没有,只需将 SharedPreferences 值保存到 int 中,然后执行++

关于android - 如何创建乐谱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36066688/

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