2017年3月20日 星期一

Android BMI計算範例

Android BMI計算範例


先上範例圖片 : 

第一頁

第二頁

第三頁


程式碼的部分 : 


第一頁的xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高 : "
            android:padding="15dp"
            android:textSize="25sp"
            android:textColor="#66B3FF"/>
        <EditText
            android:id="@+id/edtheight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="體重 : "
            android:padding="15dp"
            android:textSize="25sp"
            android:textColor="#66B3FF"/>
        <EditText
            android:id="@+id/edtweight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="BMI計算"
        android:textSize="25sp"/>

</LinearLayout>

第一頁.java :


public class MainActivity extends AppCompatActivity {

    private EditText edtheight,edtweight;
    private Button btn01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
    }
    private void findViews(){
        edtheight = (EditText)findViewById(R.id.edtheight);
        edtweight = (EditText)findViewById(R.id.edtweight);
        btn01 = (Button)findViewById(R.id.btn01);
        btn01.setOnClickListener(btn01OnClickListner);
    }
    private Button.OnClickListener btn01OnClickListner = new Button.OnClickListener(){
        public void onClick(View view){
            try {
                double height = Double.parseDouble(edtheight.getText().toString());
                double weight = Double.parseDouble(edtweight.getText().toString());
                Intent it = new Intent();
                it.setClass(MainActivity.this, Main2Activity.class);
                Bundle bundle = new Bundle();
                bundle.putDouble("height", height);
                bundle.putDouble("weight", weight);
                it.putExtras(bundle);
                startActivity(it);
            }catch (Exception e){
                Toast.makeText(MainActivity.this,"輸入錯誤",Toast.LENGTH_SHORT).show();
            }
        }
    };
}


第二頁.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.Main2Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結果 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        android:layout_marginTop="60dp">
        <Button
            android:id="@+id/btnback"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="重新計算"
            android:textSize="25sp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/btnnext"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="體位測量"
            android:textSize="25sp"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

第二頁.java :


public class Main2Activity extends AppCompatActivity {

    private Button btnback,btnnext;
    private TextView txtresult;
    double height,weight,bmi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        findViews();
    }
    private void findViews(){
        btnback = (Button)findViewById(R.id.btnback);
        btnnext = (Button)findViewById(R.id.btnnext);
        txtresult = (TextView)findViewById(R.id.result);
        btnback.setOnClickListener(btnbackOnClickListener);
        btnnext.setOnClickListener(btnnextOnClickListener);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        weight = bundle.getDouble("weight");
        calculate();
    }
    private void calculate(){
        bmi = weight/((height*height)/10000);
        try{
            if(bmi<18 .5="" ain2activity.this="" ain3activity.class="" bmi="" btnbackonclicklistener="new" btnnextonclicklistener="new" bundle2.putdouble="" bundle2="" bundle="" button.onclicklistener="" catch="" code="" e="" else="" finish="" height="" iew="" if="" intent="" it.putextras="" it.setclass="" it="" onclick="" private="" public="" startactivity="" txtresult.settext="" view="" void="" weight="" xception="">

第三頁.xml :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="25dp"
    tools:context="tw.com.hjchen.intentbmi.Main3Activity"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="身高 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/txtheight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="體重 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/txtweight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性別 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <EditText
            android:id="@+id/sex"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入F或M"
            android:gravity="center"
            android:textSize="25sp"
            android:textColorHint="#FF0000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="腰圍 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <EditText
            android:id="@+id/waistline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="請輸入腰圍"
            android:gravity="center"
            android:textSize="25sp"
            android:textColorHint="#FF0000"/>
    </LinearLayout>

    <Button
        android:id="@+id/btncalculate"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="計算"
        android:textSize="25sp"
        android:layout_gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結果 : "
            android:textSize="25sp"
            android:padding="15dp"/>
        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="25sp"
            android:padding="15dp"/>
    </LinearLayout>

</LinearLayout>

第三頁.java :


public class Main3Activity extends AppCompatActivity {

    private TextView txtheight,txtweight,txtresult;
    private EditText sex,waistline;
    private Button btncalculate;
    private double height,weight;
    private int intwaistline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        findViews();
    }
    private void findViews(){
        txtheight = (TextView)findViewById(R.id.txtheight);
        txtweight = (TextView)findViewById(R.id.txtweight);
        txtresult = (TextView)findViewById(R.id.result);
        sex = (EditText)findViewById(R.id.sex);
        waistline = (EditText)findViewById(R.id.waistline);

        btncalculate = (Button)findViewById(R.id.btncalculate);
        Bundle bundle = getIntent().getExtras();
        height = bundle.getDouble("height");
        String shieght = String.valueOf(height);
        weight = bundle.getDouble("weight");
        String sweight = String.valueOf(weight);
        txtheight.setText(shieght);
        txtweight.setText(sweight);
        btncalculate.setOnClickListener(btncalculateListner);
    }
    private Button.OnClickListener btncalculateListner = new Button.OnClickListener(){
      public void onClick(View view){
          intwaistline = Integer.parseInt(waistline.getText().toString());
          String ssex = sex.getText().toString();
          switch (ssex){
              case "F":
                  if(intwaistline<80 break="" case="" code="" default:="" else="" if="" intwaistline="" txtresult.settext="">






沒有留言:

張貼留言