杏子和橙子 2023-03-10 17:16 采纳率: 86.4%
浏览 49
已结题

用移动开发做BMI计算器



```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>

完成了基础的搭建,但我不知道怎么得到身高和体重,并通过计算放到相应的地方去

![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/128808934876156.png "#left")
  • 写回答

3条回答 默认 最新

  • 社区专家-Monster-XH 2023-03-10 17:26
    关注

    基于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对象中。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月11日
  • 已采纳回答 3月11日
  • 修改了问题 3月10日
  • 创建了问题 3月10日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果