package com.example.test;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
private static String TAG = "test";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate");
Button submitButton = (Button)findViewById(R.id.submit);
submitButton.setOnClickListener(calculateBMI);
}
private OnClickListener calculateBMI = new OnClickListener()
{
public void onClick(View v)
{
EditText height = (EditText)findViewById(R.id.Height);
EditText weight = (EditText)findViewById(R.id.Weight);
double heightValue = Double.parseDouble(height.getText().toString()) / 100;
double weightValue = Double.parseDouble(weight.getText().toString());
double bmi = weightValue / (heightValue * heightValue);
DecimalFormat df = new DecimalFormat("0.00");
TextView result = (TextView)findViewById(R.id.result);
result.setText("Your BMI is " + df.format(bmi));
TextView advice = (TextView)findViewById(R.id.advice);
if(bmi > 25)
{
advice.setText(R.string.advice_heavy);
}
else if(bmi < 20)
{
advice.setText(R.string.advice_light);
}
else
{
advice.setText(R.string.advice_well);
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
沒有留言:
張貼留言