- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个问答应用程序,我想在每次人们标记正确答案时递增一个整数。当我处理多个 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/
我想创建一个带有一些 unicode 文本和一个音符的标签。注释如下: 我试过: titleLabel.text = @" title + ♫"; 但这会导致: 我一定是在做一些蠢事..欢
我正在使用非常基本的声音合成在我的游戏中即时创建音频和效果。基本上,我有一些方法可以在给定频率、振幅和持续时间的情况下播放声音。 对于简短的短语和旋律,我想提出一个基本的符号,以便我可以轻松地重写或将
我是一名优秀的程序员,十分优秀!