package cc.i420.bmi1; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity { EditText h; EditText w; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); h = (EditText) findViewById(R.id.height); w = (EditText) findViewById(R.id.weight); Button submit = (Button) findViewById(R.id.submit); submit.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { onClick(); } private void onClick() { if (!("".equals(h.getText().toString()) || "".equals(w.getText().toString()))) { float fh = Float.parseFloat(h.getEditableText().toString()); float fw = Float.parseFloat(w.getEditableText().toString()); float fresult; TextView result = (TextView) findViewById(R.id.result); fh = fh / 100; fh = fh * fh; NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumFractionDigits(2); fresult = fw / fh; result.setText(nf.format(fw / fh) + ""); TextView diagnosis = (TextView) findViewById(R.id.diagnosis); if (fresult < 18.5) diagnosis.setText("體重過輕!"); else if (18.5 <= fresult && fresult < 24) diagnosis.setText("正常範圍"); else if (24 <= fresult && fresult < 27) diagnosis.setText("過 重"); else if (27 <= fresult && fresult < 30) diagnosis.setText("輕度肥胖!"); else if (30 <= fresult && fresult < 35) diagnosis.setText("中度肥胖!"); else if (fresult >= 35) diagnosis.setText("重度肥胖!!!"); } } }); } }
Thursday, June 14, 2018
Android BMI
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment