package com.demo.android.bmi;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Bmi extends Activity {
private static final String TAG = "Bmi";
private static final String PREF = "BMI_PREF";
private static final String PREF_HEIGHT = "BMI_HEIGHT";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViews();
restorePrefers();
setListeners();
}
@Override
protected void onStart()
{
super.onStart();
Log.d(TAG, "onStart");
}
@Override
protected void onStop()
{
super.onStop();
Log.d(TAG, "onStop");
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit().putString(PREF_HEIGHT, field_height.getText().toString()).commit();
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.d(TAG, "onDestroy");
}
private Button button_calc;
private EditText field_height;
private EditText field_weight;
private TextView view_result;
private TextView view_advice;
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_advice = (TextView)findViewById(R.id.advice);
}
private void setListeners()
{
button_calc.setOnClickListener(calcBMI);
}
private Button.OnClickListener calcBMI = new OnClickListener()
{
public void onClick(View arg0) {
try{
Double.parseDouble(field_height.getText().toString());
Double.parseDouble(field_weight.getText().toString());
Intent intent = new Intent();
intent.setClass(Bmi.this, Report.class);
Bundle bundle = new Bundle();
bundle.putString("KEY_HEIGHT", field_height.getText().toString());
bundle.putString("KEY_WEIGHT", field_weight.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
catch(Exception err)
{
Log.e(TAG, err.toString());
Toast.makeText(Bmi.this, R.string.input_err, Toast.LENGTH_SHORT).show();
}
}
};
private void openOptionsDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.about_title)
.setMessage(R.string.about_msg)
.setPositiveButton(R.string.ok_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.setNegativeButton(R.string.homepage_label, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Uri uri = Uri.parse(getString(R.string.homepage_uri));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
})
.show();
}
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST+1;
protected static final int MUNU_FILE = Menu.FIRST+2;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
SubMenu fileMenu = menu.addSubMenu(0, MUNU_FILE, 0, "文件");
fileMenu.add(0, MENU_QUIT, 0, "退出");
menu.add(0, MENU_ABOUT, 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;
}
private void restorePrefers()
{
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
if(! "".equals(pref_height))
{
field_height.setText(pref_height);
field_weight.requestFocus();
}
}
}
- 1
- 2
前往页