```java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="BMI计算器"
android:textSize="45sp"
android:gravity="center"
android:textColor="@color/colorPrimary"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="身高(M):"
android:textSize="25sp"
/>
<EditText
android:id="@+id/height"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的身高"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="体重(KG):"
android:textSize="25sp"
/>
<EditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入您的体重"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算BMI"
android:textSize="25sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的BMI指数为:"
android:textSize="20sp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="10dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/bmi"
/>
</LinearLayout>
</LinearLayout>
完成了基础的搭建,但我不知道怎么得到身高和体重,并通过计算放到相应的地方去

基于Monster 组和GPT的调写:
为“计算BMI”按钮设置一个点击事件监听器,并在该监听器中获取用户输入的身高和体重,然后计算BMI指数并将其显示在相应的文本框中。
示例如下:
public class MainActivity extends AppCompatActivity {
private EditText mHeightEditText;
private EditText mWeightEditText;
private EditText mBmiEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHeightEditText = findViewById(R.id.height);
mWeightEditText = findViewById(R.id.weight);
mBmiEditText = findViewById(R.id.bmi);
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String heightStr = mHeightEditText.getText().toString();
String weightStr = mWeightEditText.getText().toString();
if (!TextUtils.isEmpty(heightStr) && !TextUtils.isEmpty(weightStr)) {
float height = Float.parseFloat(heightStr);
float weight = Float.parseFloat(weightStr);
float bmi = weight / (height * height);
mBmiEditText.setText(String.format("%.2f", bmi));
}
}
});
}
}
在这个代码中,先获取了输入身高和体重的EditText对象,以及用于显示BMI指数的EditText对象。然后,为“计算BMI”按钮设置了一个点击监听器,并在监听器中获取用户输入的身高和体重值。如果两个值都不为空,则将它们转换为浮点数,并计算BMI指数。最后,用setText()方法将计算出的BMI指数显示在相应的EditText对象中。
该回答引用GPTᴼᴾᴱᴺᴬᴵ
你需要在Java代码中找到你的按钮并添加一个点击监听器,然后在监听器内部实现计算逻辑,并将结果显示在EditText中。下面是一个示例代码:
public class MainActivity extends AppCompatActivity {
private EditText heightEditText;
private EditText weightEditText;
private EditText bmiEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
heightEditText = findViewById(R.id.height);
weightEditText = findViewById(R.id.weight);
bmiEditText = findViewById(R.id.bmi);
Button calculateButton = findViewById(R.id.btn);
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取身高和体重
double height = Double.parseDouble(heightEditText.getText().toString());
double weight = Double.parseDouble(weightEditText.getText().toString());
// 计算BMI
double bmi = weight / (height * height);
// 将结果显示在EditText中
bmiEditText.setText(String.format("%.2f", bmi));
}
});
}
}
在这个示例代码中,我们首先获取了输入身高和体重的EditText对象,以及用于显示计算结果的EditText对象。然后我们找到了计算按钮,并给它添加了一个点击监听器。在监听器的内部,我们从EditText中获取身高和体重的值,进行BMI计算,最后将结果显示在EditText中。注意,在进行数值转换时,需要注意异常处理,以及在显示结果时需要格式化输出。