重构重构的小例子

包com.example.ex_templete;


进口java.text.DecimalFormat;


进口android.app.Activity;

进口android.os.Bundle;

进口android.view.View;

进口android.view.View.OnClickListener;

进口android.widget.Button;

进口android.widget.EditText;

进口android.widget.TextView;


进口com.example.ex_templete.R;


公共类Bmi延伸活动{

@Override

公共空> super.oncreate (savedInstanceState);

setContentView (R.layout.activity_main);


initUI ();


}


私人空initUI () {

//监听按钮点击

=

按钮按钮(按钮)findViewById (R.id.submit);

button.setOnClickListener (calcBMI);

fieldheight=(EditText) findViewById (R.id.height);

fieldweight=(EditText) findViewById (R.id.weight);

结果=(TextView) findViewById (R.id.result);

fieldsuggest=(TextView) findViewById (R.id.suggest);}


私人> @Override

公共空> DecimalFormat nf=new DecimalFormat (" 0.00 ");

双高度=双

.parseDouble (fieldheight.getText () .toString ())/100,

双重量=双

.parseDouble (fieldweight.getText () .toString ());

双指数=重量/(高度*高度);


结果。setText(“你的BMI是”+ nf.format (BMI));


//

给健康建议如果(体重指数比;25){

fieldsuggest.setText(“太肥啦”);

}其他如果(BMI & lt;20){

fieldsuggest.setText(“很骨感”);

}其他{

fieldsuggest.setText(“身材不错”);}

}

};

私人EditText fieldheight;

私人EditText fieldweight;

私人TextView结果;

私人TextView fieldsuggest;

}



xml中只需要两个EditText,两个TextView,一个按钮即可

& lt; RelativeLayout xmlns: android=" http://schemas.android.com/apk/res/android "

,,xmlns:工具=" http://schemas.android.com/tools "

,,android: layout_width=" match_parent "

,,android: layout_height=" match_parent "

,,android: paddingBottom=" @dimen/activity_vertical_margin "

,,android: paddingLeft=" @dimen/activity_horizontal_margin "

,,android: paddingRight=" @dimen/activity_horizontal_margin "

,,android: paddingTop=" @dimen/activity_vertical_margin "

,,工具:上下文=" com.example.ex_templete。MainActivity”在


,,& lt;按钮

,,,,android: id=癅 + id/提交”

,,,,android: layout_width=" wrap_content "

,,,,android: layout_height=" wrap_content "

,,,,android: layout_alignParentBottom=" true "

,,,,android: layout_centerHorizontal=" true "

,,,,android: layout_marginBottom=" 134 dp "

,,,,android:文本="提交“/祝辞


,,EditText & lt;

,,,,android: id=癅 + id/高度”

,,,,android: layout_width=" wrap_content "

,,,,android: layout_height=" wrap_content "

,,,,android: layout_alignParentLeft=" true "

,,,,android: layout_alignParentTop=" true "

,,,,android: ems=" 10 "/祝辞


,,EditText & lt;

,,,,android: id=癅 + id/重量”

,,,,android: layout_width=" wrap_content "

,,,,android: layout_height=" wrap_content "

,,,,android: layout_alignLeft=癅 + id/高度”

,,,,android: layout_below=癅 + id/高度”

,,,,android: layout_marginTop=" 28 dp "

,,,,android: ems=" 10 "在


,,,,& lt; requestFocus/祝辞

,,& lt;/EditText>


,,& lt; TextView

,,,,android: id=癅 + id/结果”

,,,,android: layout_width=" wrap_content "

,,,,android: layout_height=" wrap_content "

,,,,android: layout_alignLeft=癅 + id/重量”

,,,,android: layout_below=癅 + id/重量”

,,,,android: layout_marginTop=" 30 dp "

,,,,android:文本="大型文本"

,,,,android: textAppearance=" ?android: attr/textAppearanceLarge”/祝辞


,,& lt; TextView

,,,,android: id=癅 + id/建议”

,,,,android: layout_width=" wrap_content "

,,,,null

重构重构的小例子