重构Refactor的小例子
package com.example.ex_templete;
站在用户的角度思考问题,与客户深入沟通,找到翠屏网站设计与翠屏网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:做网站、成都网站建设、企业官网、英文网站、手机端网站、网站推广、国际域名空间、网站空间、企业邮箱。业务覆盖翠屏地区。
import java.text.DecimalFormat;
import android.app.Activity;
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;
import com.example.ex_templete.R;
public class Bmi extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
}
private void initUI() {
// Listen for button clicks
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
fieldheight = (EditText) findViewById(R.id.height);
fieldweight = (EditText) findViewById(R.id.weight);
result = (TextView) findViewById(R.id.result);
fieldsuggest = (TextView) findViewById(R.id.suggest);
}
private OnClickListener calcBMI = new OnClickListener() {
@Override
public void onClick(View v) {
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);
result.setText("Your BMI is " + nf.format(BMI));
// Give health advice
if (BMI > 25) {
fieldsuggest.setText("太肥啦");
} else if (BMI < 20) {
fieldsuggest.setText("很骨感");
} else {
fieldsuggest.setText("身材不错");
}
}
};
private EditText fieldheight;
private EditText fieldweight;
private TextView result;
private TextView fieldsuggest;
}
xml中只需要两个editText,两个textview,一个按钮即可
xmlns:tools="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" tools:context="com.example.ex_templete.MainActivity" >
本文名称:重构Refactor的小例子
网址分享:http://ybzwz.com/article/ipdhde.html