如何在 Android 上将我的分数从一项活动转移到另一项活动?

发布于 2024-11-28 05:20:08 字数 538 浏览 0 评论 0原文

这就是我在一项活动中使用整数的方式。这是一个匹配类型的问题,相应的单选按钮是答案之一。正确的单选按钮将给出 1 分。

Integer score1;
public void onCheckedChanged(RadioGroup group, int rb1) {
  switch(rb1){
  case R.id.radioButton1:
     score1=0;
     break;
  case R.id.radioButton2:
     score1=0;
     break;
  case R.id.radioButton3:
     score1=0;
     break;  
  case R.id.radioButton4:
     score1= 1;
     break;  
}

在结果屏幕上,我将使用如下整数:

totalscore = score1 +score2 .....

如何将 score1 从带有单选按钮的活动传输到结果屏幕的活动?

This is how I used my integer from one activity. It's a matching type question and the corresponding radio button is one of the answers. The correct radiobutton would give the score of 1.

Integer score1;
public void onCheckedChanged(RadioGroup group, int rb1) {
  switch(rb1){
  case R.id.radioButton1:
     score1=0;
     break;
  case R.id.radioButton2:
     score1=0;
     break;
  case R.id.radioButton3:
     score1=0;
     break;  
  case R.id.radioButton4:
     score1= 1;
     break;  
}

On the result screen I will be usng the integer like this:

totalscore = score1 +score2 .....

How do I transfer score1 from the activity with the radio buttons to the activity for the result screen?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-12-05 05:20:08

尝试

Integer score1, totalscore;

public void onCheckedChange(RadioGroup group, int rb1) {
    switch (rb1) {
        case R.id.radioButton1:
            score1=0;
            break;
        case R.id.radioButton2:
            score1=0;
            break;
        case R.id.radioButton3:
            score1=0;
            break;  
        case R.id.radioButton4:
            score1= 1;
            totalscore += 1;
            break;  
     }
}

Try

Integer score1, totalscore;

public void onCheckedChange(RadioGroup group, int rb1) {
    switch (rb1) {
        case R.id.radioButton1:
            score1=0;
            break;
        case R.id.radioButton2:
            score1=0;
            break;
        case R.id.radioButton3:
            score1=0;
            break;  
        case R.id.radioButton4:
            score1= 1;
            totalscore += 1;
            break;  
     }
}
星軌x 2024-12-05 05:20:08

首先,您可以大大简化您的 switch 逻辑:

Integer score1;
public void onCheckedChanged(RadioGroup group, int rb1) {
    score1 = (rb1 == R.id.radioButton4) ? 1 : 0;
}

其次,有多种不同的方法可以将 score1 从一个 Activity 传递到另一个 Activity。例如,当您为第二个 Activity 创建 Intent 时,您可以使用 putExtra() 来存储您的分数值,然后第二个 Activity 可以使用 getExtra () 启动时读取值。

或者,您可以使用任何一种快速但有问题的技巧,例如将 score1 设为 public static 字段,或通过系统属性传递它,或将其写入商定的文件位置,或将其存储到数据库中商定的字段(这些黑客仅在每个设备只有一个活动实例的情况下才有效,并且实际上根本不推荐)。

实际上,您应该坚持使用 getExtra()putExtra()。沿着以下路线:

//in QuestionActivity
private Integer score1;

//...

public void onCheckedChanged(RadioGroup group, int rb1) {
    score1 = (rb1 == R.id.radioButton4) ? 1 : 0;
    Intent resultIntent = new Intent(this, ResultActivity.class);
    resultIntent.putExtra("score1", score1);
    startActivity(resultIntent);
}


//in ResultActivity
private Integer score1;

//...

@Override
protected void onStart() {
    score1 = this.getIntent().getExtras().getInt("score1");
    super.onStart();
}

First off, you can greatly simplify your switch logic:

Integer score1;
public void onCheckedChanged(RadioGroup group, int rb1) {
    score1 = (rb1 == R.id.radioButton4) ? 1 : 0;
}

Second, there are several different ways to pass score1 from one Activity to another. For instance, when you create your Intent for the second Activity, you can use putExtra() to store your score value, and then the second Activity can use getExtra() to read the value when it starts.

Or you can use any of a number of quick but questionable hacks, such as making score1 a public static field, or passing it around via system properties, or writing it out to an agreed-upon file location, or storing it to an agreed-upon field in a database (these hacks only work if there is only a single instance of your activity per device, and are really not recommended at all).

Really though you should just stick with getExtra() and putExtra(). Along the lines of:

//in QuestionActivity
private Integer score1;

//...

public void onCheckedChanged(RadioGroup group, int rb1) {
    score1 = (rb1 == R.id.radioButton4) ? 1 : 0;
    Intent resultIntent = new Intent(this, ResultActivity.class);
    resultIntent.putExtra("score1", score1);
    startActivity(resultIntent);
}


//in ResultActivity
private Integer score1;

//...

@Override
protected void onStart() {
    score1 = this.getIntent().getExtras().getInt("score1");
    super.onStart();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文