重构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" >

  

    android:id="@+id/submit"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentBottom="true"

    android:layout_centerHorizontal="true"

    android:layout_marginBottom="134dp"

    android:text="提交" />

  

    android:id="@+id/height"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignParentLeft="true"

    android:layout_alignParentTop="true"

    android:ems="10" />

  

    android:id="@+id/weight"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/height"

    android:layout_below="@+id/height"

    android:layout_marginTop="28dp"

    android:ems="10" >

    

  

  

    android:id="@+id/result"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/weight"

    android:layout_below="@+id/weight"

    android:layout_marginTop="30dp"

    android:text="Large Text"

    android:textAppearance="?android:attr/textAppearanceLarge" />

  

    android:id="@+id/suggest"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_alignLeft="@+id/result"

    android:layout_below="@+id/result"

    android:layout_marginTop="25dp"

    android:text="Large Text"

    android:textAppearance="?android:attr/textAppearanceLarge" />

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:重构Refactor的小例子-创新互联
分享链接:http://pwwzsj.com/article/dipjhi.html