在Android中传递整数活动

问题描述:

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

这里的活性2的代码:

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(); 
     } 
} 
} 

之前启动你的意图,通过临时演员。这个问题是一个重复,并已经有一个详细的解决方案:How do I pass data between Activities in Android application?

+2

请不要发表一个答案,因为问题确实是重复的 - 只需写一条评论或者将其标记为重复:) – 0X0nosugar

当你创建一个Intent到下一个活动,请致电:

yourIntent.putExtra("points", finalpoints); 

,并把你的finalpoints变量,然后在接下来的活动检索使用:

points = getIntent().getIntExtra("points"); 

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