首页 > 解决方案 > 如何从一个活动中检索 int 数据并将其设置为另一个活动的 textView

问题描述

发送类代码:

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

标签: javaandroidandroid-intent

解决方案


在发送方,使用 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+"");

推荐阅读