Android Java-使用意图将值传递给第二个活动

时间:2019-06-01 18:27:50

标签: java android android-intent

我正在编写一个可跟踪您的体重和BMI的Android应用。为了第一次更新或设置两个值,有第二个活动,允许我输入应该传递给第一个活动的值。

我尝试了几种方法来实现这一目标,我最接近的是以下代码:

第二活动:(UpdateProgressActivity.java)

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

    Intent intentWeight = getIntent();
    String WeightText = intentWeight.getStringExtra(EXTRA_MESSAGE);
    TextView messageWeight = findViewById(R.id.weight);
    messageWeight.setText(WeightText);

    Intent intentBMI = getIntent();
    String BMIText = intentBMI.getStringExtra(EXTRA_MESSAGE);
    TextView messageBMI = findViewById(R.id.bmi);
    messageBMI.setText(BMIText);


    updateButton = findViewById(R.id.updateBodyProgress);
    updateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openActivityUpdateProgress();
        }
    });

}

第一个活动:(BodyProgressActivity.java)

public class UpdateProgressActivity extends AppCompatActivity {

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


    Button confirmProgress;
    confirmProgress = findViewById(R.id.confirmBodyStatus);
    confirmProgress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            returnToActivityProgress();
        }
    });

}

protected void returnToActivityProgress() {
    Intent intent = new Intent(this, BodyProgressActivity.class);
    startActivity(intent);
}

public void createStatus(View view) {
    EditText messageWeight = findViewById(R.id.textNewWeight);
    String textWeight = messageWeight.getText().toString();
    Intent intentWeight = new Intent(Intent.ACTION_SEND); intentWeight.setType("text/plain");
    intentWeight.putExtra(Intent.EXTRA_TEXT, textWeight);
    String textWeigthTemp = getString(R.string.currentWeight);
    Intent chosenIntentWeight = Intent.createChooser(intentWeight, textWeigthTemp);
    startActivity(chosenIntentWeight);

    EditText messageBMI = findViewById(R.id.textNewWeight);
    String textBMI = messageBMI.getText().toString();
    Intent intentBMI = new Intent(Intent.ACTION_SEND); intentBMI.setType("text/plain");
    intentWeight.putExtra(Intent.EXTRA_TEXT, textBMI);
    String textBMITemp = getString(R.string.currentBMI);
    Intent chosenIntentBMI = Intent.createChooser(intentWeight, textBMITemp);
    startActivity(chosenIntentBMI);
}

}

我从第二个活动的“ textNewWeight ”和“ textNewBMI ”中获取值,必须将其传递给“ weight ”和“ bmi ”。 我在第一个活动中得到的只是空白。

1 个答案:

答案 0 :(得分:2)

In the first activity. Use this code. We are using putExtra method to send the data to next activity through key and value. Key is the 1st parameter and value is the 2nd parameter. Key name can be your choice and it is used to retrieve the data in the 2nd activity. Value is the data which you want ti send to next activity.     

    Intent i=new Intent(firstactivity.this,secondactivity.class);
     String weight=messageWeight.getText().toString();
    i.putExtra(“weight”,weight);
   startActivity(i);


In the second activity,use this to recieve the data. 

   Bundle b=Intent.getExtras();
    String recievedWeight=b.getString(“weight”);

the parameter name in the b.getString(“”) must be same as which you have declared in the 1st activity.