package cn.edu.nju.software.BMICalc;
import java.text.DecimalFormat;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//import android.widget.Toast;
public class BMIactivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.bmi_main);
setFont();
setViews();
setListeners();
}
private void setFont() {
// TODO Auto-generated method stub
TextView tv;
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");
tv=(TextView)findViewById(R.id.htv);
tv.setTypeface(face);
tv=(TextView)findViewById(R.id.wtv);
tv.setTypeface(face);
tv=(TextView)findViewById(R.id.comment_title);
tv.setTypeface(face);
tv=(TextView)findViewById(R.id.result_title);
tv.setTypeface(face);
tv=(TextView)findViewById(R.id.go);
tv.setTypeface(face);
}
Button go;
EditText height;
EditText weight;
TextView result;
TextView comment;
private void setListeners() {
// TODO Auto-generated method stub
go.setOnClickListener(l);
}
OnClickListener l=new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
InputMethodManager im=(InputMethodManager) go.getContext().getSystemService(INPUT_METHOD_SERVICE);
im.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
DecimalFormat df=new DecimalFormat("0.00");
double h=Double.parseDouble(height.getText().toString())/100;
double w=Double.parseDouble(weight.getText().toString());
double bmi= w/(h*h);
result.setText(getString(R.string.result)+df.format(bmi));
if(bmi<18)
comment.setText(R.string.toolight);
else if(bmi>24)
comment.setText(R.string.tooheavy);
else
comment.setText(R.string.ok);
}
};
private void setViews() {
// TODO Auto-generated method stub
go=(Button) findViewById(R.id.go);
height=(EditText) findViewById(R.id.height);
weight=(EditText) findViewById(R.id.weight);
result=(TextView) findViewById(R.id.result);
comment=(TextView) findViewById(R.id.comment);
}
}
评论0