在Android中的活动之间传递整数

时间:2017-07-08 15:11:15

标签: java android android-layout

我正在创建一个测验应用。粘贴的代码是应用程序的第二个活动。但它表示int point = 0,并且没有说明终点,因为它取决于测验中的答案。 但是,如果我将此活动中的任何答案传递给下一个和下一个。

这是activity2的代码:

 package com.example.android.questionme;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;


public class Main2Activity extends AppCompatActivity {
    Intent intent = getIntent();

    int point = 0;
    int finalpoints;


    RadioButton option1forQ1;
    RadioButton option2forQ1;
    RadioButton option1forQ2;
    RadioButton option2forQ2;
    RadioButton option3forQ2;
    RadioButton option4forQ2;
    RadioButton option1forQ3;
    RadioButton option2forQ3;
    RadioButton option3forQ3;
    RadioButton option1forQ4;
    RadioButton option2forQ4;
    RadioButton option3forQ4;
    RadioButton option4forQ4;
    RadioButton option1forQ5;
    RadioButton option2forQ5;
    RadioButton option3forQ5;
    RadioButton option4forQ5;
    CheckBox option1forQ6;
    CheckBox option2forQ6;
    CheckBox option3forQ6;
    CheckBox option4forQ6;
    RadioButton option1forQ7;
    RadioButton option2forQ7;
    RadioButton option3forQ7;
    RadioButton option4forQ7;
    RadioButton option1forQ8;
    RadioButton option2forQ8;
    RadioButton option3forQ8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        option1forQ1 = (RadioButton) findViewById(R.id.right_answer_for_Q1);
        option2forQ1 = (RadioButton) findViewById(R.id.wrong_answer_for_Q1);
        option1forQ2 = (RadioButton) findViewById(R.id.question_two_right_answer);
        option2forQ2 = (RadioButton) findViewById(R.id.question_two_wrong_answer0);
        option3forQ2 = (RadioButton) findViewById(R.id.question_two_wrong_answer1);
        option4forQ2 = (RadioButton) findViewById(R.id.question_two_wrong_answer2);
        option1forQ3 = (RadioButton) findViewById(R.id.question_three_right_answer);
        option2forQ3 = (RadioButton) findViewById(R.id.question_three_wrong_answer0);
        option3forQ3 = (RadioButton) findViewById(R.id.question_three_wrong_answer1);
        option1forQ4 = (RadioButton) findViewById(R.id.question_four_wrong_answer0);
        option2forQ4 = (RadioButton) findViewById(R.id.question_four_right_answer);
        option3forQ4 = (RadioButton) findViewById(R.id.question_four_wrong_answer1);
        option4forQ4 = (RadioButton) findViewById(R.id.question_four_wrong_answer2);
        option1forQ5 = (RadioButton) findViewById(R.id.question_five_wrong_answer0);
        option2forQ5 = (RadioButton) findViewById(R.id.question_five_wrong_answer1);
        option3forQ5 = (RadioButton) findViewById(R.id.question_five_wrong_answer2);
        option4forQ5 = (RadioButton) findViewById(R.id.question_five_right_answer);
        option1forQ6 = (CheckBox) findViewById(R.id.question_six_right_answer);
        option2forQ6 = (CheckBox) findViewById(R.id.question_six_wrong_answer);
        option3forQ6 = (CheckBox) findViewById(R.id.question_six_wrong_answer1);
        option4forQ6 = (CheckBox) findViewById(R.id.question_six_right_answer1);
        option1forQ7 = (RadioButton) findViewById(R.id.question_seven_wrong_answer);
        option2forQ7 = (RadioButton) findViewById(R.id.question_seven_wrong_answer1);
        option3forQ7 = (RadioButton) findViewById(R.id.question_seven_right_answer);
        option4forQ7 = (RadioButton) findViewById(R.id.question_seven_wrong_answer2);
        option1forQ8 = (RadioButton) findViewById(R.id.question_eight_wrong_answer);
        option2forQ8 = (RadioButton) findViewById(R.id.question_eight_right_answer);
        option3forQ8 = (RadioButton) findViewById(R.id.question_eight_wrong_answer2);
    }
    public void onYesRadioButtonClicked (View view){
        Intent intent = new Intent(this, Main3Activity.class);
        boolean option1forQ1IsChecked = option1forQ1.isChecked();
        boolean option2forQ1IsChecked = option2forQ1.isChecked();
        if (option1forQ1IsChecked) {
            point *= 2;
            startActivity(intent);
            Toast.makeText(this, "Good Start"+ "2 Points", Toast.LENGTH_SHORT).show();
        }
        else
        if (option2forQ1IsChecked) {
            point -= 1;
        }
    }
    public void onNoRadioButtonClicked (View view){
        boolean option2forQ1IsChecked = option2forQ1.isChecked();
        if (option2forQ1IsChecked){
        Toast.makeText(this, "Wrong Answer, You can Google about Google on Google"
                , Toast.LENGTH_SHORT).show();
        }
}
}

2 个答案:

答案 0 :(得分:0)

在启动你的意图之前,传递额外内容。这个问题是重复的,并且已经存在详细的解决方案:How do I pass data between Activities in Android application?

答案 1 :(得分:0)

当您为下一个活动创建意图时,请致电:

yourIntent.putExtra("points", finalpoints);

并将你的终点变量放到它,然后在下一个活动中使用:

检索它
points = getIntent().getIntExtra("points");

这是一个很好的例子,但是有一个字符串。但是,方法是一样的。 https://developer.android.com/training/basics/firstapp/starting-activity.html