package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Bmi extends Activity {
/** Called when the activity is first created. */
private EditText fieldheight;
private EditText fieldweight;
private TextView viewresult;
private TextView viewsuggest;
private Button button_cal;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
setListener();
}
private void findView(){
button_cal=(Button)findViewById(R.id.submit);
fieldheight=(EditText)findViewById(R.id.heigth);
fieldweight=(EditText)findViewById(R.id.weigth);
viewresult=(TextView)findViewById(R.id.result);
viewsuggest=(TextView)findViewById(R.id.suggest);
}
private void setListener(){
button_cal.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI=new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
DecimalFormat nf=new DecimalFormat("0.00");
double height =Double.parseDouble(fieldheight.getText().toString())/100;
double weight=Double.parseDouble(fieldweight.getText().toString());
double BMI=weight/(height*height);
viewresult.setText("你的BMI值是"+nf.format(BMI));
if(BMI>25){
viewsuggest.setText(R.string.advice_hevry);
}else if(BMI<20){
viewsuggest.setText(R.string.advice_light);
}else{
viewsuggest.setText(R.string.advice_average);
}
openOptionsDialog();
}
private void openOptionsDialog() {
// TODO Auto-generated method stub
}
};
}
评论0