iT邦幫忙

2021 iThome 鐵人賽

DAY 3
0
Mobile Development

刮掉Web Development的我,與撿到的Android Development系列 第 3

[Lesson3] BMI

今天要來做一個計算BMI的簡易App!

activity_main.xml:
使用LinearLayout的vertical進行排版,再將元件一一放入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/high"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="身高:"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/getHigh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal"/>

    <TextView
        android:id="@+id/weight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="體重:"
        android:textSize="20dp" />

    <EditText
        android:id="@+id/getWeight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/btnClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="btnClick"
        android:text="送出" />
</LinearLayout>

MainActivity:
按下按鈕時判斷身高或體重有沒有空值,如果都沒有空值的話,進行計算與跳頁傳值的動作

public class MainActivity extends AppCompatActivity {
    EditText getHigh,getWeight;
    float result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getHigh = findViewById(R.id.getHigh);
        getWeight = findViewById(R.id.getWeight);
    }
    public void btnClick(View view) {
        if (!("".equals(getHigh.getText().toString()) || "".equals(getWeight.getText().toString()))){
            float high = Float.parseFloat(getHigh.getText().toString());
            float weight = Float.parseFloat(getWeight.getText().toString());
            float highBMI = (high/100)*(high/100);

            result = weight/highBMI;

            Intent it = new Intent(MainActivity.this, MainActivity2.class);
            Bundle bundle = new Bundle();
            bundle.putFloat("BMI", result);
            it.putExtras(bundle);
            startActivity(it);
            finish();
        } else {
            Toast.makeText(this,"請輸入身高或體重", Toast.LENGTH_SHORT).show();
        }
    }
}

activity_main2.xml:
跟activity_main.xml一樣,使用LinearLayout的vertical進行排版,再將元件一一放入

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    
    <TextView
        android:id="@+id/console"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="console"/>

    <TextView
        android:id="@+id/check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="check"/>
        
    <Button
        android:id="@+id/btnBack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="btnBack"
        android:text="返回" />
</LinearLayout>

MainActivity2:
顯示MainActivity計算過的BMI結果,判斷BMI的程度

public class MainActivity2 extends AppCompatActivity {
    TextView console,check;
    float result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        Bundle bundle = getIntent().getExtras();
        result = bundle.getFloat("BMI");

        NumberFormat nf = NumberFormat.getInstance();   // 數字格式
        nf.setMaximumFractionDigits(2);                 // 限制小數第二位

        console = findViewById(R.id.console);
        console.setText(nf.format(result));

        checkBMI();
    }
    private void checkBMI() {
        check = findViewById(R.id.check);
        if (result < 18.5)
            check.setText("過輕");
        else if (18.5 <= result && result < 24)
            check.setText("正常");
        else if (24 <= result && result < 27)
            check.setText("過重");
        else if (27 <= result && result < 30)
            check.setText("輕胖");
        else if (30 <= result && result < 35)
            check.setText("中胖");
        else if (result >= 35)
            check.setText("重胖");
    }
    public void btnBack(View view) {
        Intent it = new Intent(MainActivity2.this, MainActivity.class);
        startActivity(it);
        finish();
    }
}

謝謝大家願意花時間閱讀,小弟弟我在此鞠躬/images/emoticon/emoticon41.gif


上一篇
[Lesson2] Android Studio安裝
下一篇
[Lesson4] SharedPreferences
系列文
刮掉Web Development的我,與撿到的Android Development30

尚未有邦友留言

立即登入留言