如何在 Android 上将我的分数从一项活动转移到另一项活动?
这就是我在一项活动中使用整数的方式。这是一个匹配类型的问题,相应的单选按钮是答案之一。正确的单选按钮将给出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
首先,您可以大大简化您的
switch
逻辑:其次,有多种不同的方法可以将
score1
从一个 Activity 传递到另一个 Activity。例如,当您为第二个 Activity 创建Intent
时,您可以使用putExtra()
来存储您的分数值,然后第二个 Activity 可以使用getExtra ()
启动时读取值。或者,您可以使用任何一种快速但有问题的技巧,例如将
score1
设为public static
字段,或通过系统属性传递它,或将其写入商定的文件位置,或将其存储到数据库中商定的字段(这些黑客仅在每个设备只有一个活动实例的情况下才有效,并且实际上根本不推荐)。实际上,您应该坚持使用
getExtra()
和putExtra()
。沿着以下路线:First off, you can greatly simplify your
switch
logic:Second, there are several different ways to pass
score1
from one Activity to another. For instance, when you create yourIntent
for the second Activity, you can useputExtra()
to store your score value, and then the second Activity can usegetExtra()
to read the value when it starts.Or you can use any of a number of quick but questionable hacks, such as making
score1
apublic 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()
andputExtra()
. Along the lines of: