码迷,mamicode.com
首页 > 其他好文 > 详细

重构BMI

时间:2015-12-27 19:07:38      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

运用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     };

 

重构BMI

标签:

原文地址:http://www.cnblogs.com/youling1894/p/5080560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!