程序效果如如下:
第一个界面HelloworldActivity,主界面,获取身高、体重后计算BMI值,代码如下:
package com.helloworld;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;
public class HelloworldActivity extends Activity {
boolean flag = true;
Button btn_jmptest, btn_calculate;
// TextView text_result, text_suggest;
EditText edit_height, edit_weight;
Spinner spinner_background;
protected int PREF_BgPicNum;
protected static final int MENU_ABOUT = Menu.FIRST;
protected static final int MENU_QUIT = Menu.FIRST + 1;
private static final String PREF = "BMI_PREF";
private static final String PREF_HEIGHT = "BMI_PREF";
private static final String PREF_BGPIC = "BMI_BG";
//private
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Resources resources = getBaseContext().getResources();
// Drawable myDrawable =
resources.getDrawable(R.drawable.icon);
findViews();
restorePrefs();
SetListener();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_ABOUT, 0,
"关于").setIcon(R.drawable.help_browser);
menu.add(0, MENU_QUIT, 0,
"结束").setIcon(R.drawable.emblem_unreadable);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case MENU_ABOUT:
openOptionDailog();
break;
case MENU_QUIT:
finish();
break;
}
return true;
}
@Override
public void onPause()
{
super.onPause();
SharedPreferences settings = getSharedPreferences(PREF, 0);
settings.edit().putString(PREF_HEIGHT,
edit_height.getText().toString()).commit();
settings.edit().putInt(PREF_BGPIC, PREF_BgPicNum).commit();
}
public void findViews() {
//btn_jmptest = (Button) findViewById(R.id.btn_jmptest);
btn_calculate = (Button) findViewById(R.id.btn_calculate);
// text_result = (TextView)
findViewById(R.id.textView_result);
// text_suggest = (TextView)
findViewById(R.id.textView_suggest);
edit_height = (EditText) findViewById(R.id.editText_height);
edit_weight = (EditText) findViewById(R.id.editText_weight);
spinner_background = (Spinner) findViewById(R.id.spinner_bg);
ArrayAdapter adapter_bg
= ArrayAdapter.createFromResource(this, R.array.bgPic,
android.R.layout.simple_spinner_item);
adapter_bg.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner_background.setAdapter(adapter_bg);
}
public void SetListener() {
//btn_jmptest.setOnClickListener(jmpBtnCL);
btn_calculate.setOnClickListener(calcBMI);
spinner_background.setOnItemSelectedListener(spinner_getBg);
}
public void calculate() {
double result;
try {
double height = Double
.parseDouble(edit_height.getText().toString()) / 100;
double weight = Double
.parseDouble(edit_weight.getText().toString());
result = weight / (height * height);
Intent intent = new Intent(HelloworldActivity.this,
Report.class);
Bundle bundle = new Bundle();
bundle.putDouble("result", result);
intent.putExtras(bundle);
startActivity(intent);
//HelloworldActivity.this.finish();
} catch (Exception e) {
Toast.makeText(HelloworldActivity.this, "输入错误,只接受数字和小数点!",
Toast.LENGTH_SHORT).show();
}
}
public void openOptionDailog() {
AlertDialog.Builder ab = new AlertDialog.Builder(
HelloworldActivity.this);
ab.setTitle("About Android BMI");
ab.setMessage("Android BMI Calc ver1.0\n@copyright jie");
ab.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
ab.setNegativeButton("Url", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
Uri uri = Uri.parse(getString(R.string.url));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
ab.show();
}
public void restorePrefs()
{
SharedPreferences settings = getSharedPreferences(PREF, 0);
String pref_height = settings.getString(PREF_HEIGHT, "");
edit_height.setText(pref_height);
PREF_BgPicNum = settings.getInt(PREF_BGPIC, 0);
LinearLayout temp = (LinearLayout)findViewById(R.id.ui_main);
switch(PREF_BgPicNum)
{
case 0:
temp.setBackgroundResource(R.drawable.background0);
break;
case 1:
temp.setBackgroundResource(R.drawable.background1);
break;
case 2:
temp.setBackgroundResource(R.drawable.background2);
break;
}
if(!pref_height.equals(""))
{
edit_weight.requestFocus();
}
}
private Button.OnClickListener calcBMI = new
Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
calculate();
}
};
//测试用,暂无用处
private Button.OnClickListener jmpBtnCL = new
Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
jump();
}
};
//测试用,暂无用处
private Button.OnClickListener dlgBtnCL = new
Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
openOptionDailog();
}
};
private Spinner.OnItemSelectedListener spinner_getBg = new
Spinner.OnItemSelectedListener()
{
public void
onItemSelected(AdapterView> arg0,
View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(flag)
{
arg0.setSelection(PREF_BgPicNum);
flag = false;
return;
}
PREF_BgPicNum = arg0.getSelectedItemPosition();
setBGPic(PREF_BgPicNum);
}
public void
onNothingSelected(AdapterView>
arg0) {
// TODO Auto-generated method stub
}
};
public void setBGPic(int i)
{
LinearLayout temp = (LinearLayout)findViewById(R.id.ui_main);
switch(i)
{
case 0:
temp.setBackgroundResource(R.drawable.background0);
break;
case 1:
temp.setBackgroundResource(R.drawable.background1);
break;
case 2:
temp.setBackgroundResource(R.drawable.background2);
break;
}
}
//测试用,暂无用处
protected void jump() {
// TODO Auto-generated method stub
}
}
第二个界面,Report类,显示计算结果,代码如下:
package com.helloworld;
import java.text.DecimalFormat;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Report extends Activity {
TextView text_result, text_suggest;
Button btn_back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
findViews();
btmSetListener();
Bundle bundle = this.getIntent().getExtras();
Double result = bundle.getDouble("result");
DecimalFormat df = new DecimalFormat("0.00");
Toast.makeText(Report.this, "计算完毕!", Toast.LENGTH_SHORT)
.show();
showNotification(result);
text_result.setText(getText(R.string.bmi_result) +
df.format(result));
if (result > 25) {
text_suggest.setText(R.string.advice_heavy);
} else if (result < 20) {
text_suggest.setText(R.string.advice_light);
} else {
text_suggest.setText(R.string.advice_average);
}
}
public void findViews() {
text_result = (TextView) findViewById(R.id.textView_result);
text_suggest = (TextView)
findViewById(R.id.textView_suggest);
btn_back = (Button) findViewById(R.id.btn_back);
}
public void btmSetListener() {
btn_back.setOnClickListener(jmpBtnCL);
}
private Button.OnClickListener jmpBtnCL = new
Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(Report.this,
HelloworldActivity.class);
//startActivity(intent);
Report.this.finish();
}
};
public void showNotification(double r)
{
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification barMsg;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, HelloworldActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
if (r > 25) {
barMsg = new Notification(android.R.drawable.stat_sys_warning ,
"哎呀,过重了", System.currentTimeMillis());
barMsg.setLatestEventInfo(this, "BMI值过高", "通知监督",
contentIntent);
nm.notify(0, barMsg);
} else if (r < 20) {
barMsg = new Notification(android.R.drawable.stat_sys_warning ,
"嗯,太轻了", System.currentTimeMillis());
barMsg.setLatestEventInfo(this, "BMI值正常", "通知监督",
contentIntent);
nm.notify(0, barMsg);
} else {
barMsg = new Notification(android.R.drawable.stat_sys_warning ,
"哦,正常", System.currentTimeMillis());
barMsg.setLatestEventInfo(this, "BMI值过低", "通知监督",
contentIntent);
nm.notify(0, barMsg);
}
}
}