【问题标题】:incrementing a value upon radio button click between 5 activities, 5th activity shows total of correct answers在 5 个活动之间单击单选按钮时增加一个值,第 5 个活动显示正确答案的总数
【发布时间】: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


【解决方案1】:

如下创建一个 AppSingelton 类:

public class AppSingleton {

    public static int score= 0;
}

现在在每个 Activity 中将其值递增如下:

if(optionCorrectSeleceted()){
AppSingelton.score=AppSingelton.score+5;
}

其中 optionCorrectSelected 将是您的方法,如果答案正确,则返回类型应为 true,否则为 false。 在您想要显示分数的最后一个活动中,只需使用...txtView.setText("Score :"+AppSingelton.score);

【讨论】:

  • 先生,当我输入我的代码时,代码有错误。它说我需要将“final”添加到公共静态 int。我把 if(optionCorrectSeleceted()){ AppSingelton.score=AppSingelton.score+5;在单选按钮上,但它给出了错误。 }
  • np 将其更改为 public static final int score= 0;在 AppSingleton 类中
  • 先生,我很困惑,“其中 optionCorrectSelected 将是您的方法,如果答案正确,则返回类型应为 true,否则为 false”,先生,我如何创建该方法?我试过了,但做错了..
猜你喜欢
  • 1970-01-01
  • 2015-05-26
  • 2013-06-08
  • 2012-02-12
  • 2015-09-30
  • 2021-05-22
  • 2013-06-30
  • 2020-11-28
  • 2013-04-10
相关资源
最近更新 更多