繁体   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