package my.BMI;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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);
//Listen for button clicks
findViews();
setListensers();
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_suggest;
private void findViews()
{
button_calc = (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_suggest = (TextView) findViewById(R.id.suggest);
}
private void setListensers()
{
button_calc.setOnClickListener(calcBMI);
}
private Button.OnClickListener calcBMI = new Button.OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf = new DecimalFormat("0.00");
double height = Double.parseDouble(field_height.getText().toString())/100;
double weight = Double.parseDouble(field_weight.getText().toString());
double BMI = weight / (height * height);
view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
if(BMI>25)
{
view_suggest.setText(R.string.advice_heavy);
}
else if(BMI<20)
{
view_suggest.setText(R.string.advice_light);
}
else
{
view_suggest.setText(R.string.advice_average);
}
//openOptionsDialog();
}
};
private void openOptionsDialog()
{
/**/
new AlertDialog.Builder(BMIActivity.this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton
(R.string.ok_label,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
}
}
)
.setNegativeButton
(R.string.homepage_label,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialoginterface, int i)
{
Uri uri = Uri.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}
)
.show();
//Toast.makeText(BMIActivity.this, "BMI 计算器", Toast.LENGTH_SHORT).show();
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_Quit = Menu.FIRST+1;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ABOUT, 0, "关于...");
menu.add(0, MENU_Quit, 0, "结束");
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case MENU_ABOUT:
openOptionsDialog();
break;
case MENU_Quit:
finish();
break;
}
return true;
}
}
评论0