以下是实现上述功能的基本步骤:
界面1:
1. 创建一个包含性别选择、身高输入和提交按钮的界面。
2. 在提交按钮的点击事件中,获取用户选择的性别和输入的身高,并将其作为参数传递给第二个界面。
界面2:
1. 创建一个用于显示标准体重的文本框。
2. 在界面2的onCreate方法中,获取从界面1传递过来的性别和身高参数。
3. 根据性别和身高计算标准体重,并将结果显示在文本框中。
4. 在返回结果按钮的点击事件中,将计算出的标准体重作为参数传递回界面1。
界面1:
1. 在onActivityResult方法中,获取从界面2传递回来的标准体重参数,并将其显示在界面1中。
下面是示例代码:
界面1:
```java
public class MainActivity extends AppCompatActivity {
private RadioGroup genderRadioGroup;
private EditText heightEditText;
private static final int REQUEST_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
genderRadioGroup = findViewById(R.id.gender_radio_group);
heightEditText = findViewById(R.id.height_edit_text);
Button submitButton = findViewById(R.id.submit_button);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int genderId = genderRadioGroup.getCheckedRadioButtonId();
String gender = "";
if (genderId == R.id.male_radio_button) {
gender = "male";
} else if (genderId == R.id.female_radio_button) {
gender = "female";
}
int height = Integer.parseInt(heightEditText.getText().toString());
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("gender", gender);
intent.putExtra("height", height);
startActivityForResult(intent, REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST