我如何从一个活动中检索int数据并将其设置为另一活动的textView

问题描述 投票:0回答:1

发送课程代码:

Intent i = new Intent(this, QuizActivity.class);
        i.putExtra("name", name);
        startActivity(i);

正在接收课程代码:

Intent intent = getIntent();
    if (intent != null) {
        totalScore = intent.getIntExtra("totalScore",0);
    }
    TextView scoreView = findViewById(R.id.totalScore);
    scoreView.setText(totalScore);
java android android-intent
1个回答
0
投票

在发送方,使用Intent.putExtra:

Intent myIntent = new Intent(A.this, B.class);
myIntent.putExtra("totalScore", intValue);
startActivity(myIntent);

在接收方,使用Intent.getIntExtra:

 Intent mIntent = getIntent();
 int totalScore = mIntent.getIntExtra("totalScore", 0);
 TextView scoreView = findViewById(R.id.totalScore);
 scoreView.setText(totalScore+"");
© www.soinside.com 2019 - 2024. All rights reserved.