gpt4 book ai didi

java - 如何使用按钮将字符串值从一个 Activity 传递到两个不同的 Activity ?

转载 作者:行者123 更新时间:2023-12-01 14:23:56 26 4
gpt4 key购买 nike

我想通过单击按钮将从 EditText 获得的值传递给两个 Activity 。我设法将值传递给一个 Activity ,将其显示在 TextView 中,我想对另一个 Activity 执行相同的操作,但我不确定 switch case 是否是正确的使用方法,因为只有一个按钮。

这是第一个 Activity 的代码:

public class ProfInfo1 extends Activity {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profinfo1);


final EditText et0 = (EditText) findViewById (R.id.et_name);
final EditText et1 = (EditText) findViewById (R.id.et_age);
final EditText et2 = (EditText) findViewById (R.id.et_height);
final EditText et3 = (EditText) findViewById (R.id.et_weight);

Button back = (Button) findViewById(R.id.btn_back1);
back.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent a = new Intent(ProfInfo1.this, WelcomeActivity.class);
startActivity(a);
}

});

Button next = (Button) findViewById(R.id.btn_next1);
next.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=new Intent(ProfInfo1.this, ViewProf1.class);
intent.putExtra("name", et0.getText().toString());
intent.putExtra("age", et1.getText().toString());
intent.putExtra("height", et2.getText().toString());
intent.putExtra("weight", et3.getText().toString());
startActivity(intent);
}
});


}
}

这是显示值的第二个 Activity :

public class ViewProf1 extends Activity {
EditText age, height, weight;
String a, h, w;
Float age1, height1, weight1;
float bmi, cal;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profinfo1view);

TextView nameView = (TextView) findViewById (R.id.view_name);
nameView.setText("Hello " + getIntent().getExtras().getString("name") + ",");
TextView ageView = (TextView) findViewById (R.id.view_age);
ageView.setText("Age: " + getIntent().getExtras().getString("age"));
TextView heightView = (TextView) findViewById (R.id.view_height);
heightView.setText("Your current height is " + getIntent().getExtras().getString("height") + " m.");
TextView weightView = (TextView) findViewById (R.id.view_weight);
weightView.setText("Your current weight is " + getIntent().getExtras().getString("weight") + " kg.");

a = getIntent().getExtras().getString("age");
h = getIntent().getExtras().getString("height");
w = getIntent().getExtras().getString("weight");

age1 = Float.valueOf(a).floatValue();
height1 = Float.valueOf(h).floatValue();
weight1 = Float.valueOf(w).floatValue();

//bmi calculation
bmi = (float) (weight1/Math.pow(height1, 2));
TextView showresult = (TextView) findViewById (R.id.view_bmi);
showresult.setText("Your BMI is " + Float.valueOf(bmi) + ".");

//cal intake calculation
cal = (float) (((655 + (9.6 * weight1) + (1.8 * height1) - (4.7 * age1)) * 1.2) - 550);
TextView showcal = (TextView) findViewById (R.id.view_cal);
showcal.setText("Your ideal daily calories intake is " + Float.valueOf(cal) + ".");

//next button
Button next2 = (Button) findViewById (R.id.btn_next2);
next2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent a = new Intent(ViewProf1.this, FoodDiary.class);
startActivity(a);
}

});

//back button
Button back2 = (Button) findViewById (R.id.btn_back2);
back2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Intent a = new Intent(ViewProf1.this, ProfInfo1.class);
startActivity(a);
}

});


}

第三个 Activity :

public class CompareWeight extends Activity {

EditText weight;
String w;
Float weight1;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.compareweight);

TextView weightView = (TextView) findViewById (R.id.test);
weightView.setText("Your previous weight is " + getIntent().getExtras().getString("weight") + " kg.");

w = getIntent().getExtras().getString("weight");

weight1 = Float.valueOf(w).floatValue();
}
}

我只想将权重值传递给第三个 Activity 。

最佳答案

您一次只能将值传递给一个 Activity 。但是如果您想将权重值传递给第三个 Activity ,那么您可以在第一个 Activity 中设置它的值并在第三个 Activity 中检索它。所以基本上我的意思是

  1. 借助 putExtra 将您想要传递的值放入 SecondActivity。
  2. 设置sharedPreference 变量。
  3. startActivity( Intent );

关于java - 如何使用按钮将字符串值从一个 Activity 传递到两个不同的 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17314645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com