Android开发初体验 - 体质指数BMI.Calc

这两天终于下定决心把书看起来了,在搭好开发环境后接触了第一个除了Hello World的Android初学小例,对基于Eclipse的Android编程有了一个框架性的粗浅认识。

使用的书是人民邮电出版社《Google Android开发入门指南(第二版)》,在此感谢BUPT图书馆。
菜鸟踏出了第一步!


开发手记:

版本v1.8.6.3.40

功能更新:

1.增加MENU菜单

2.修改关于对话框和一键登录微博功能移至菜单

3.增加菜单中退出程序功能

4.修改计算结果和建议移至下一面板

5.超重时状态栏提示功能





版本v1.8.5.10.52

功能更新:

1.增加输入框为空时用户提示

2.增加按钮呼出关于对话框功能

3.增加一键登录微博功能

Android开发初体验 - 体质指数BMI.Calc_第1张图片

Android开发初体验 - 体质指数BMI.Calc_第2张图片



版本v1.8.4.21.11

测试平台:Android1.5 HVGA

功能特性:

1.计算常人体质指数并给出建议

Android开发初体验 - 体质指数BMI.Calc_第3张图片


JAVA源代码

版本v1.8.6.3.40

//Bmi.java
package com.demo.android.bmi;

import java.net.URISyntaxException;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Bmi 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
		// findViewById方法取得按钮组件,为保持与XML文件描述类型一致作强制转换
		Button button = (Button) findViewById(R.id.submit);
		button.setOnClickListener(calcBMI);
	}

	private OnClickListener calcBMI = new OnClickListener() {
		public void onClick(View v) {
			/*
			 * DecimalFormat nf = new DecimalFormat("0.00");// 定义双精度浮点数 
			 * try {
			 * EditText fieldheight = (EditText) findViewById(R.id.height);// 取数
			 * EditText fieldweight = (EditText) findViewById(R.id.weight);// 取数
			 * double height = Double.parseDouble(fieldheight.getText()
			 * .toString()) / 100; double weight =
			 * Double.parseDouble(fieldweight.getText() .toString()); double BMI
			 * = weight / (height * height);
			 * 
			 * TextView result = (TextView) findViewById(R.id.result);
			 * result.setText("您的体质指数BMI值为:" + nf.format(BMI)); //
			 * 这里我一开始用"@string/bmi_result",后来用R.string.bmi_result,结果都不行,估计和+号有关系
			 * 
			 * TextView fieldsuggest = (TextView) findViewById(R.id.suggest);//
			 * 取得控件 if (BMI > 25) { fieldsuggest.setText(R.string.advice_heavy);
			 * } else if (BMI < 20) {
			 * fieldsuggest.setText(R.string.advice_light); } else {
			 * fieldsuggest.setText(R.string.advice_average); } } catch
			 * (Exception obj) { Toast.makeText(Bmi.this, "真傲娇,两个空都要填满的!",
			 * Toast.LENGTH_LONG) .show(); // 自动隐去型面包板,作用于提示很友好 } }
			 */
			// 以上第一版第二版点击按钮计算BMI值
			// 以下第三版点击按钮跳转到报告页面
			
			EditText field_height = (EditText) findViewById(R.id.height);// 取数
			EditText field_weight = (EditText) findViewById(R.id.weight);// 取数
			if("".equals(field_height.getText().toString())||"".equals(field_weight.getText().toString()))
				{
				Toast.makeText(Bmi.this, "别傲娇,两个空都要填满的!",Toast.LENGTH_LONG) .show();
				}
			else{
			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);
			}
		}
	};
	protected static final int MENU_ABOUT = Menu.FIRST;
	protected static final int MENU_Quit = Menu.FIRST + 1;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {// 创建菜单
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_ABOUT, 0, "关于...").setIcon(
				android.R.drawable.ic_menu_info_details);
		menu.add(0, MENU_Quit, 0, "退出").setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {// 处理菜单选项动作
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case MENU_ABOUT:
			OpenOptionsDialog();
			break;
		case MENU_Quit:
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void OpenOptionsDialog() {
		new AlertDialog.Builder(Bmi.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));
								// 注意:这里网址一定要有http://,否则坑爹的FOCUS又来
								Intent intent = new Intent(Intent.ACTION_VIEW,
										uri);
								startActivity(intent);
							}
						})// 添加去往主页按钮
				.show();// 显示
	}
}

//Report.java
package com.demo.android.bmi;

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;

public class Report extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.report);
		findViews();
		showResults();
		setListensers();
	}

	private Button button_back;
	private TextView view_result;
	private TextView view_suggest;

	private void findViews() {
		button_back = (Button) findViewById(R.id.report_back);
		view_result = (TextView) findViewById(R.id.result_r);
		view_suggest = (TextView) findViewById(R.id.suggest_r);
	}

	// Listen for button clicks
	private void setListensers() {
		button_back.setOnClickListener(backMain);
	}

	private Button.OnClickListener backMain = new Button.OnClickListener() {
		public void onClick(View v) {
			// Close this Activity
			Report.this.finish();
		}
	};

	private void showResults() {
		DecimalFormat nf = new DecimalFormat("0.00");
		Bundle bunde = this.getIntent().getExtras();
		// Activity.getIntent()可以得到传来的Intent对象实体
		// getExtras()取得附加在Intent上的bunde对象
		double height = Double.parseDouble(bunde.getString("KEY_HEIGHT")) / 100;
		double weight = Double.parseDouble(bunde.getString("KEY_WEIGHT"));
		double BMI = weight / (height * height);
		view_result.setText(getString(R.string.bmi_result) + nf.format(BMI));

		// Give health advice
		if (BMI > 25) {
			showNotification(BMI);
			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);
		}
	}

	protected void showNotification(double BMI) {
		// TODO Auto-generated method stub
		NotificationManager barManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//取得系统内置提供的功能
		Notification barMsg = new Notification(android.R.drawable.stat_sys_warning, "欢迎来到微胖界!",
				System.currentTimeMillis());//立刻显示
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,//用户点选状态栏中该笔信息时执行一个Activity
				new Intent(this, Bmi.class), PendingIntent.FLAG_UPDATE_CURRENT);//第三个参数最重要,定义要调用的Intent
		barMsg.setLatestEventInfo(Report.this, "您的BMI值过高", "通知铁道部",//出现在状态栏下拉详细通知里
				contentIntent);
		barManager.notify(0, barMsg);//传送信息内容给框架并显示在状态栏
	}
}



版本v1.8.5.10.52

package com.demo.android.bmi;

import java.net.URISyntaxException;
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.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 {
	/** Called when the activity is first created. */
	@Override
	// 重写函数
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);// 原方法调用
		setContentView(R.layout.main);// 显示在屏幕上

		// Listen for button clicks
		// findViewById方法取得按钮组件,为保持与XML文件描述类型一致作强制转换
		Button button = (Button) findViewById(R.id.submit);
		button.setOnClickListener(calcBMI);
	}

	private OnClickListener calcBMI = new OnClickListener() {
		public void onClick(View v) {
			DecimalFormat nf = new DecimalFormat("0.00");// 定义双精度浮点数
			try {
				EditText fieldheight = (EditText) findViewById(R.id.height);// 取数
				EditText fieldweight = (EditText) findViewById(R.id.weight);// 取数
				double height = Double.parseDouble(fieldheight.getText()
						.toString()) / 100;
				double weight = Double.parseDouble(fieldweight.getText()
						.toString());
				double BMI = weight / (height * height);

				TextView result = (TextView) findViewById(R.id.result);
				result.setText("您的体质指数BMI值为:" + nf.format(BMI));
				// 这里我一开始用"@string/bmi_result",后来用R.string.bmi_result,结果都不行,估计和+号有关系

				TextView fieldsuggest = (TextView) findViewById(R.id.suggest);// 取得控件
				if (BMI > 25) {
					fieldsuggest.setText(R.string.advice_heavy);
				} else if (BMI < 20) {
					fieldsuggest.setText(R.string.advice_light);
				} else {
					fieldsuggest.setText(R.string.advice_average);
				}
				OpenOptionsDialog();// 用于每次计算完BMI显示建议后顺便调用关于对话框
			} catch (Exception obj) {
				Toast.makeText(Bmi.this, "真傲娇,两个空都要填满的!", Toast.LENGTH_LONG)
						.show();
				// 自动隐去型面包板,作用于提示很友好
			}
		}

		private void OpenOptionsDialog() {
			new AlertDialog.Builder(Bmi.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));
									// 注意:这里网址一定要有http://,否则坑爹的FOCUS又来
									Intent intent = new Intent(
											Intent.ACTION_VIEW, uri);
									startActivity(intent);
								}
							})// 添加去往主页按钮
					.show();// 显示
		}
	};
}



v1.8.4.21.11

package com.demo.android.bmi;
import java.text.DecimalFormat;//功能:将数字格式化成特定的字符串格式

import android.app.Activity;//Android程序必包含这俩
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//主类
public class Bmi 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
        //findViewById方法取得按钮组件,为保持与XML文件描述类型一致作强制转换
        Button button = (Button)findViewById(R.id.submit);
		button.setOnClickListener(calcBMI);
    }
    
    private OnClickListener calcBMI = new OnClickListener(){
    	public void onClick(View v){
    		DecimalFormat nf = new DecimalFormat("0.00");//定义双精度浮点数
    		EditText fieldheight = (EditText)findViewById(R.id.height);//取数
    		EditText fieldweight = (EditText)findViewById(R.id.weight);//取数
    		double height = Double.parseDouble(fieldheight.getText().toString())/100;
    		double weight = Double.parseDouble(fieldweight.getText().toString());
    		double BMI = weight/(height*height);
    		
    		TextView result = (TextView)findViewById(R.id.result);
    		result.setText("您的体质指数BMI值为:"+nf.format(BMI));//这里我一开始用"@string/bmi_result",后来用R.string.bmi_result,结果都不行
    		
    		TextView fieldsuggest = (TextView)findViewById(R.id.suggest);//取得控件
    		if(BMI>25){
    			fieldsuggest.setText(R.string.advice_heavy);
    		}else if(BMI<20){
    			fieldsuggest.setText(R.string.advice_light);
    		}else{
    			fieldsuggest.setText(R.string.advice_average);    			
    		}
    	}
    };
}

main.xml源代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/height"
    />
<EditText 
	android:id="@+id/height" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal"
    android:text=""
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/weight"
    />
<EditText 
	android:id="@+id/weight" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="numberDecimal"
    android:text=""
    />
<Button  
	android:id="@+id/submit" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/bmi_btn"
    />
<TextView 
	android:id="@+id/result" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/bmi_result"
    />
<TextView
	android:id="@+id/suggest" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    />

</LinearLayout>



你可能感兴趣的:(exception,android,layout,Class,button,menu)