您好我正在尝试创建一个应用程序,用户通过radiobuttons选择答案。如果单击radiobutton,则有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);
}
}
的strings.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>
答案 0 :(得分:0)
创建一个AppSingelton类,如下所示:
public class AppSingleton {
public static int score= 0;
}
现在在每个Activity中增加其值如下:
if(optionCorrectSeleceted()){
AppSingelton.score=AppSingelton.score+5;
}
其中optionCorrectSelected将是您的方法,如果answer为正确,则返回类型应为true,否则为false。
在您想要显示分数的最终活动中,只需使用... txtView.setText("Score :"+AppSingelton.score);