简体   繁体   English

如何在其他活动中发送正确答案和所选答案?

[英]How can i send the correct answer and the chosen answer in another activity?

GenEdQuestion.class The mode of quiz is multiple choice. GenEdQuestion.class测验模式是多项选择。 I want to save or transfer the correct answer and the chosen answer to another activity (result). 我想保存正确的答案或将选择的答案转移到其他活动(结果)。 I transferred the score but i don't know how to transfer the result. 我转移了分数,但我不知道如何转移结果。 How can I do that? 我怎样才能做到这一点?

      answer1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer1.getText() == mAnswer){
                mNumber++;
                mScore++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }
            return false;
        }
    });

    answer2.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer2.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }
            return false;
        }
    });

    answer3.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer3.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }
            return false;
        }
    });

    answer4.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if (answer4.getText() == mAnswer){
                mNumber++;
                number.setText(" "+mNumber+" .");
                mScore++;
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }

            }else{
                mNumber++;
                number.setText(" "+mNumber+" .");
                if (mNumber == quizCount){
                    onFinish();
                }
                else{
                    updateQuestion(r.nextInt(mQuestionsLength));
                }
            }
            return false;
        }
    });


   new CountDownTimer(60000*60, 1000){
        public void onTick(long secondsLeft){
            mCountDownGenEd.setText(""+String.format("%d min, %d sec",
                    TimeUnit.MILLISECONDS.toMinutes(secondsLeft),
                    TimeUnit.MILLISECONDS.toSeconds(secondsLeft) -
            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(secondsLeft))));

        }
        public void onFinish(){
            Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
            intent.putExtra("this", mScore);
            startActivity(intent);
        }
    }.start();
}

private void onFinish() {
    Intent intent = new Intent(getApplicationContext(), GenEdQuestionsResult.class);
    intent.putExtra("this", mScore);
    startActivity(intent);
}

private void updateQuestion(int i) {

    question.setText(mQuestions.getQuestion(i));
    answer1.setText(mQuestions.getChoice1(i));
    answer2.setText(mQuestions.getChoice2(i));
    answer3.setText(mQuestions.getChoice3(i));
    answer4.setText(mQuestions.getChoice4(i));
    mAnswer = mQuestions.getCorrectAnswer(i);

} }

GenEdQuestionsResult.class This activity will show the score and if the user passed or failed. GenEdQuestionsResult.class此活动将显示分数以及用户是否通过或失败。 How will i put the result here? 我如何将结果放在这里?

    mFinalScore = (TextView) findViewById(R.id.scoreTextView);
    mResult = (TextView) findViewById(R.id.resultTextView);
    int score = getIntent().getIntExtra("this", 0);
    mFinalScore.setText(score+" / 20");
    if (score <= 10){mResult.setText("Failed");}
    else{mResult.setText("Passed");}

This is the result i want to create 这是我要创建的结果

this is the picture of result i want to create 这是我要创建的结果的图片

Three ways, shared preferences or getter and setter methods, or putextra when starting a new activity like below: 三种方式,共享首选项或getter和setter方法,或在开始以下新活动时使用putextra:

Shared preferences 共享的首选项

Setting values in Preference: 在首选项中设置值:

// MY_PREFS_NAME - a static String variable like: 
//public static final String MY_PREFS_NAME = "MyPrefsFile";
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();

Retrieve data from preference: 从首选项中检索数据:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
}

Referenced to : https://stackoverflow.com/a/23024962/4674760 引用至: https : //stackoverflow.com/a/23024962/4674760

Getter/Setter in a new activity 参加新活动的Getter / Setter

String newenq="New Enquiry";
public void setNewenq(String newenq) {
    this.newenq = newenq;
}

public String getNewenq() {
    return newenq;

}

PutExtra PutExtra

String easyPuzzle  = "answer";
Intent i = new Intent(this, ToClass.class);
i.putExtra("epuzzle", easyPuzzle);
startActivity(i)

you have 3 approaches to do this the first one=> make a static variable and then you can access it where ever you want , 您有3种方法来做到这一点,第一种=>制作一个静态变量,然后您可以在任何需要的地方访问它,

the second one you can => use intent putExtra 第二个你可以=>使用意图putExtra

Intent i = new Intent(this, SecondClass.class);
i.putExtra("answer", "myAnswer");
startActivity(i)

the third approach is using a sharedPreference take a look at this link https://developer.android.com/reference/android/content/SharedPreferences.html 第三种方法是使用sharedPreference看看此链接https://developer.android.com/reference/android/content/SharedPreferences.html

Use shared preference like this: 使用共享首选项,如下所示:

public static final String answer = "answer"; //declare this globally at the top.
SharedPreferences prefs = getSharedPreferences(Commonclass.ANSWER,Context.MODE_PRIVATE);
SharedPreferences.Editor editor;
editor = prefs.edit();
String a = prefs.getString("answer","answer"); //to get the stored value
editor.putString(theme,"blue"); // to store your answer
editor.commit();

Create a Commonclass: 创建一个通用类:

public class Commonclass 
{
  public static final String ANSWER = "answer" ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM