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.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");
findViews();
setListener();
}
private Button button_calculate;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_advice;
private void findViews()
{
button_calculate = (Button)findViewById(R.id.submit);
field_height = (EditText)findViewById(R.id.Height);
field_weight = (EditText)findViewById(R.id.Weight);
view_result = (TextView)findViewById(R.id.result);
view_advice = (TextView)findViewById(R.id.advice);
}
private void setListener()
{
button_calculate.setOnClickListener(calculateBMI);
}
private Button.OnClickListener calculateBMI = new Button.OnClickListener()
{
public void onClick(View arg0) {
double height = Double.parseDouble(field_height.getText().toString()) / 100;
double weight = Double.parseDouble(field_weight.getText().toString());
double result = weight / (height * height);
DecimalFormat df = new DecimalFormat("0.00");
view_result.setText(getText(R.string.bmi_result) + df.format(result));
if(result > 25)
{
view_advice.setText(R.string.advice_heavy);
}
else if(result < 20)
{
view_advice.setText(R.string.advice_light);
}
else
{
view_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;
}
}
沒有留言:
張貼留言