簡體   English   中英

如何將 integer 值從一項活動轉移到另一項活動?

[英]How can I transfer integer value from one activity to another?

我正在嘗試從 4 個不同的活動中編譯 4 個整數。 第一個活動是 4 integer 之一。第二個活動是我編譯它們的地方。我不知道從不同活動發送值的最佳方式是什么。 我看到的大多數意圖方法都使用 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();}

更新這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM