简单的BMI计算器
MainActivity.java
public class MainActivity extends Activity {
Spinner s1,s2;
EditText e1,e2;
Button b1;
private String danwei = "G"; // 英制:E
private String biaozhun = "C"; // 国际:G
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.e1);
e2=(EditText) findViewById(R.id.e2);
s1=(Spinner) findViewById(R.id.s1);
s2=(Spinner) findViewById(R.id.s2);
b1=(Button) findViewById(R.id.b1);
//s1定义数据源
String[] arr1={"公制","英制"};
ArrayAdapter<String> ad1=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice,arr1);
s1.setAdapter(ad1);
s1.setPrompt("度量单位");
s1.setOnItemSelectedListener(new spinnerSelectedListener());
//s2定义数据源
String[] arr2={"中国标准","国际标准"};
ArrayAdapter<String> ad2=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice,arr2);
s2.setAdapter(ad2);
s2.setPrompt("BMI标准");
s2.setOnItemSelectedListener(new spinner2SelectedListener());
//按钮事件
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {tz();}});
}
//按钮事件
public void tz(){
String h=e1.getText().toString();
String w=e2.getText().toString();
//判断输入内容是否为空
if(h==null||w==null||h.equals("")||w.equals("")){
Toast.makeText(MainActivity.this,"提醒:输入为空",Toast.LENGTH_SHORT).show();
return;
}
//初始化身高体重
double dh=1,dw=1;
try {
//把tv字符串转化为double并赋值
dh=Double.parseDouble(h);
dw=Double.parseDouble(w);
} catch (Exception e) {
Toast.makeText(MainActivity.this,"提醒:输入非法",Toast.LENGTH_SHORT).show();
return;
}
//计算
dh/=100;
//单位是公制
double r=dw/(dh*dh);
//如果单位是英制
if(danwei.equals("E")){
r*=0.0703;
}
//保留2位小数点
DecimalFormat df=new DecimalFormat("#.00");
String tr=df.format(r);
//传递参数到第二个页面
Intent intent=new Intent();
intent.setClass(this,Activity02.class);
intent.putExtra("r", tr);
intent.putExtra("bz", biaozhun);
startActivity(intent);
}
//第一个监听事件
private class spinnerSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String select=parent.getItemAtPosition(position).toString();
//根据选项设置et的提示词(hint)
if(select.equals("公制")){
e1.setHint("身高 单位:厘米cm");
e2.setHint("体重 单位:千克kg");
danwei="G";}
else if(select.equals("英制")){
e1.setHint("身高 单位:英寸in");
e2.setHint("体重 单位:英镑lb");
danwei="E";}
}
public void onNothingSelected(AdapterView<?> parent) {}
}
//第二个监听事件
private class spinner2SelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String select=parent.getItemAtPosition(position).toString();
//根据选项设置et的提示词(hint)
if(select.equals("中国标准"))
{biaozhun="C";
Toast.makeText(MainActivity.this,"提醒:中国标准",Toast.LENGTH_SHORT).show();}
else if(select.equals("国际标准"))
{biaozhun="G";
Toast.makeText(MainActivity.this,"提醒:国际标准",Toast.LENGTH_SHORT).show();}
}
public void onNothingSelected(AdapterView<?> parent) {}
}
}
activity_main.xml
<?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:weightSum="5"
android:orientation="vertical" >
<Spinner
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:spinnerMode="dialog"
android:id="@+id/s1"
/>
<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="请输入身高"
/>
<EditText
android:id="@+id/e2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="请输入体重"
/>
<Spinner
android:id="@+id/s2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:spinnerMode="dialog"
/>
<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="BMI"/>
</LinearLayout>
Activity02.java
public class Activity02 extends Activity {
String bz,zt,sjy;
String sbmi;
double dbmi=1;
TextView bmi,jy;
ImageView tp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
bmi=(TextView) findViewById(R.id.bmi);
jy=(TextView) findViewById(R.id.jy);
tp=(ImageView) findViewById(R.id.tp);
Intent intent=getIntent();
bz=intent.getStringExtra("bz");
sbmi=intent.getStringExtra("r");
dbmi=Double.valueOf(sbmi);
//判断
if(bz.equals("C")){
tp.setImageResource(R.drawable.e);
if(dbmi<=18.4){zt="偏瘦";sjy="多吃东西";}
else if(dbmi<=23.9){zt="正常";sjy="继续保持";}
else if(dbmi<=27.9){zt="过重";sjy="少吃东西";}
else {zt="肥胖";sjy="少吃东西";}
}
else{
tp.setImageResource(R.drawable.g);
if(dbmi<=16.4){zt="很瘦";sjy="多吃东西";}
else if(dbmi<=18.4){zt="偏瘦";sjy="多吃东西";}
else if(dbmi<=24.9){zt="正常";sjy="继续保持";}
else if(dbmi<=29.9){zt="过重";sjy="少吃东西";}
else if(dbmi<=34.9){zt="一类肥胖";sjy="少吃东西";}
else if(dbmi<=39.0){zt="二类肥胖";sjy="少吃东西";}
else {zt="三类肥胖";sjy="少吃东西";}
}
bmi.setText("您的BMI:"+dbmi+" "+"身体状态:建议"+zt);
jy.setText(sjy);
}
}
activity02.xml
<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:orientation="vertical"
android:weightSum="5"
tools:context="com.example.bmi.Activity02" >
<ImageView
android:id="@+id/tp"
android:layout_weight="2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="@drawable/g"
/>
<TextView
android:id="@+id/bmi"
android:layout_weight="1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="bmi" />
<TextView
android:id="@+id/jy"
android:layout_weight="2"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="建议" />
</LinearLayout>