运用MVC模式,将逻辑声明和界面声明分为若干个方法,看上去会更方便。作为一个强迫症患者很喜欢哈哈哈哈
1 public class BMI extends ActionBarActivity { 2 private Button btn_bmi; 3 private EditText filed_height; 4 private EditText filed_weight; 5 private TextView result; 6 private TextView suggest; 7 private double height; 8 private double weight; 9 private double bmi; 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_bmi); 14 findView(); 15 setListene(); 16 } 17 private void findView(){ 18 btn_bmi=(Button) findViewById(R.id.submit); 19 filed_height=(EditText) findViewById(R.id.height); 20 filed_weight=(EditText) findViewById(R.id.weight); 21 result=(TextView) findViewById(R.id.result); 22 suggest=(TextView) findViewById(R.id.suggest); 23 } 24 private void setListene() { 25 // TODO Auto-generated method stub 26 btn_bmi.setOnClickListener(the_bmi); 27 28 } 29 private Button.OnClickListener the_bmi= new OnClickListener() { 30 31 @Override 32 public void onClick(View v) { 33 // TODO Auto-generated method stub 34 35 height=Double.parseDouble(filed_height.getText().toString())/100; 36 weight=Double.parseDouble(filed_weight.getText().toString()); 37 bmi=weight/(height*height); 38 39 40 DecimalFormat df=new DecimalFormat("0.00"); 41 42 result.setText("你的BMI值为"+df.format(bmi)); 43 44 suggest=(TextView) findViewById(R.id.suggest); 45 if(bmi>25) 46 suggest.setText(R.string.advice_heavy); 47 else if(bmi<20) 48 suggest.setText(R.string.advice_light); 49 else 50 suggest.setText(R.string.advice_average); 51 } 52 };
时间: 2024-10-13 01:14:04