【发布时间】:2014-03-25 12:47:11
【问题描述】:
您好,我正在尝试制作一个用户通过单选按钮选择答案的应用程序。如果单击单选按钮,则有 5 个问题,第二个活动从一个新问题开始,一直持续到最后一个问题。如何设置上一个活动的正确答案总分?
这是我的代码:(我真的需要帮助,我是 android 初学者)
第一个问题有 5 个..
package com.example.kei;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
public static int counter=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void choice_a(View view){
Intent intent = new Intent(this, Page_two.class);
startActivity(intent);
}
public void choice_b(View view){
counter++;
Intent intent = new Intent(this, Page_two.class);
startActivity(intent);
}
public void choice_c(View view){
Intent intent = new Intent(this, Page_two.class);
startActivity(intent);
}
}
最后一页:
package com.example.kei;
import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;
public class Total extends Activity {
TextView totalScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_total);
totalScore.setText(MainActivity.counter);
}
}
字符串.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">kei</string>
<string name="action_settings">Settings</string>
<string name="total">Total: </string>
<string name="score"> 0 </string>
<string name="outOf"> /5 </string>
<!-- QUESTION number1 -->
<string name="question1">1.) The company that spearheaded the development of android operating system.</string>
<string name="q1_choice_a">Microsoft</string>
<string name="q1_choice_b">Google</string>
<string name="q1_choice_c">Apple</string>
<!-- QUESTION number2 -->
<string name="question2">2.) The first publicly available smartphone running the Android OS.</string>
<string name="q2_choice_a">Samsung Galaxy</string>
<string name="q2_choice_b">Google Nexus</string>
<string name="q2_choice_c">HTC Dream</string>
<!-- QUESTION number3 -->
<string name="question3">3.) The virtual machine which enables installation and running of application in android device.</string>
<string name="q3_choice_a">Dalvik VM</string>
<string name="q3_choice_b">Java Runtime Environment</string>
<string name="q3_choice_c">.Net Framework</string>
<!-- QUESTION number4 -->
<string name="question4">4.) OHA is a consortion of hardware, software and telecom companies devoted to advancing open standards for mobile devices. What does the acronym OHA stands for?</string>
<string name="q4_choice_a">Overseas Housing Allowance</string>
<string name="q4_choice_b">Ontario Hockey Asssociation</string>
<string name="q4_choice_c">Open Handset Alliance</string>
<!-- QUESTION number5 -->
<string name="question5">5.) Android share of the global smartphone market as of 3rd qtr, 2013.</string>
<string name="q5_choice_a">40%</string>
<string name="q5_choice_b">58%</string>
<string name="q5_choice_c">81%</string>
<string name="title_activity_page_two">Page_two</string>
<string name="title_activity_page_three">Page_three</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_page_four">Page_four</string>
<string name="title_activity_page_five">Page_five</string>
<string name="title_activity_total">Total</string>
</resources>
【问题讨论】:
-
使用静态 int 并从每个活动中递增。如果你愿意,你可以把它放在自己的类中或任何活动中
-
对于每个问题,您有 1 个活动吗?如果是这样,则不需要相同
-
@Raghunandan 每个问题都是一个活动,我想在每个正确答案的值上加一个并显示最后一个活动的总数
-
@JosePerez 没有必要这样做。您可以在同一屏幕上替换新问题的内容。计算同一活动中的分数并在下一个活动中显示分数。如果你有 100 个问题,那么你不能有 100 个活动是没有意义的
-
好的,我明白你是对的,先生
标签: android android-intent sharedpreferences