java 如何将整数值从一个活动转移到另一个活动?

66bbxpm5  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(120)

我正在尝试编译4个不同Activity中的4个整数。第一个Activity是4个整数中的一个。第二个Activity是我编译它们的地方。我不知道从不同Activity发送值的最佳方式是什么。我看到的大多数Intent方法都使用startActivity,但仍然无法工作。

public class QuizSecond extends AppCompatActivity implements View.OnClickListener{
TextView totalQuestionsTextView2;
TextView questionTextView2;
Button ansA2, ansB2, ansC2, ansD2;
Button submitBtn2;
int score= 0;
int totalQuestion2 = QuestionAnswer2.question2.length;
int currentQuestionIndex2 = 0;
String selectedAnswer2 = "";

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

    totalQuestionsTextView2 = findViewById(R.id.total_question2);
    questionTextView2 = findViewById(R.id.question_preview);
    ansA2 = findViewById(R.id.ans_A2);
    ansB2 = findViewById(R.id.ans_B2);
    ansC2 = findViewById(R.id.ans_C2);
    ansD2 = findViewById(R.id.ans_D2);
    submitBtn2 = findViewById(R.id.submit_btn2);

    ansA2.setOnClickListener(this);
    ansB2.setOnClickListener(this);
    ansC2.setOnClickListener(this);
    ansD2.setOnClickListener(this);
    submitBtn2.setOnClickListener(this);

    totalQuestionsTextView2.setText("Total questions : "+totalQuestion2);
    loadNewQuestion();

}

@Override
public void onClick(View view) {

    ansA2.setBackgroundColor(Color.WHITE);
    ansB2.setBackgroundColor(Color.WHITE);
    ansC2.setBackgroundColor(Color.WHITE);
    ansD2.setBackgroundColor(Color.WHITE);

    Button clickedButton = (Button) view;
    if(clickedButton.getId()==R.id.submit_btn2){
        if(selectedAnswer2.equals(QuestionAnswer2.correctAnswers2[currentQuestionIndex2])) {
            score++;

        }
        currentQuestionIndex2++;
        loadNewQuestion();

        Intent quizIntent = new Intent(QuizSecond.this,ComputeActivity.class);
        quizIntent.putExtra("EXTRA_NUMBER",score);


    }
    else{
        //choices button clicked
        selectedAnswer2  = clickedButton.getText().toString();
        clickedButton.setBackgroundColor(Color.MAGENTA);

    }

}

void loadNewQuestion(){

    if(currentQuestionIndex2 == totalQuestion2 ){
        startActivity(new Intent(QuizSecond.this, ComputeActivity.class));

        return;
    }

    questionTextView2.setText(QuestionAnswer2.question2[currentQuestionIndex2]);
    ansA2.setText(QuestionAnswer2.choices2[currentQuestionIndex2][0]);
    ansB2.setText(QuestionAnswer2.choices2[currentQuestionIndex2][1]);
    ansC2.setText(QuestionAnswer2.choices2[currentQuestionIndex2][2]);
    ansD2.setText(QuestionAnswer2.choices2[currentQuestionIndex2][3]);

}

}
第二项活动:
int number = getIntent().getIntExtra("EXTRA_NUMBER",0); if (number > 3){ Toast.makeText(ComputeActivity.this, "Your Message", Toast.LENGHT_LONG).show();}

fnx2tebb

fnx2tebb1#

更新此

Intent quizIntent = new Intent(QuizSecond.this, ComputeActivity.class);
quizIntent.putExtra("TRANSFER_NUMBER", score);
startActivity(quizIntent);

相关问题