您的位置:首页 > 移动开发 > Android开发

Android开发--身高体重指数(BIM)计算--查看线上内容(Uri)--打开网页--重构--使用Uri查看Google地图

2012-08-09 15:40 609 查看
/* (程序头部注释开始)

* 程序的版权和版本声明部分

* Copyright (c) 2011, 烟台大学计算机学院学生

* All rights reserved.

* 文件名称:查看线上内容(Uri)

* 作 者: 雷恒鑫

* 完成日期: 2012 年 08 月 08 日

* 版 本 号: V1.0

* 对任务及求解方法的描述部分

* 输入描述:

* 问题描述:

* 程序输出:

* 程序头部的注释结束

*/

实验目标:为BMI应用程序添加一个简单的网络功能:在上一篇博文做的“openOptionsDialog”对话框函数中,新添一个“连接到首页”的按钮。

①打开网页:

我们先把“openOptionsDialog”函数中使用到的字符串添加到“res/alues/string.xml”里。因此完整的“res/alues/string.xml”文件如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BIM</string>
    <string name="height">身高(cm)</string>
    <string name="weight">体重(kg)</string>
    <string name="bmi_btn">计算 BMI 值</string>
    <string name="bmi_result">您的 BIM 值是:</string>
    <string name="about_title">关于 Android BMI</string>
    <string name="about_msg">Android BMI Calc</string>
    <string name="ok_lable">确认</string>
     <string name="input_error">打错了吗?只能输入数字喔</string>
      <string name="homepage_lable">首页</string>
</resources>


增加了“链接到首页”按钮,完整“openOptionsDialog”函数的新版程序如下:
private void openOptionsDialog(){
	//Toast.makeText(Bmi.this, "BMI 计算器", Toast.LENGTH_SHORT).show();
	new AlertDialog.Builder(Bmi.this)
	.setTitle(R.string.about_title)
	.setMessage(R.string.about_msg)
	.setPositiveButton(R.string.ok_lable,
			new DialogInterface.OnClickListener(){
		public void onClick(
			DialogInterface dialoginterface,int i){
		}
	})
	.setNegativeButton(R.string.homepage_lable, new DialogInterface.OnClickListener() {
		
		//@Override
		public void onClick(DialogInterface dialoginterface, int i) {
			// TODO Auto-generated method stub
			Uri uri = Uri.parse("http://sites.google.com/site/gasodroid/");
			Intent intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);
			
		}
	})
	.show();
	
}




运行结果:





解释:1.Uri uri = Uri.parse("sites.google.com/site/gasodroid/");创建一个Uri实体,里面包含我们要链接的网址"sites.google.com/site/gasodroid/"。

2.Intent intent = new Intent(Intent.ACTION_VIEW,uri);

这一句概括起来就是:Intent intent = new Intent(动作,内容);,通过动作实现链接的内容,并通过“startActivity”函数得到链接所指向的页面这个结果。

3.startActivity(intent);通过“startActivity”函数,Android系统即根据收到的不同“意图”(Intent)的动作和内容,打开对应的新页面或新程序。



②使用Uri查看Google地图:

方法:只要知道地点的坐标经纬度,加上一个“geo:”前缀,就可以启动“Google Map”,前往指定的坐标。代码如下:
public void onClick(DialogInterface dialoginterface, int i) {
			// TODO Auto-generated method stub
			Uri uri = Uri.parse("geo: 39.895874, 116.321238");
			final Intent intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);
			
		}





可以看到,同样是用到“ACTION_VIEW”意图,将“Uri.parse”函数的内容改变,即可用来打开不同的程序。



3.重构:

把http://www.baidu.com弄到“Resource”中统一管理。

“res/layout/main.xml”文件更新如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">BIM</string>
    <string name="height">身高(cm)</string>
    <string name="weight">体重(kg)</string>
    <string name="bmi_btn">计算 BMI 值</string>
    <string name="bmi_result">您的 BIM 值是:</string>
    <string name="about_title">关于 Android BMI</string>
    <string name="about_msg">Android BMI Calc</string>
    <string name="ok_lable">确认</string>
     <string name="input_error">打错了吗?只能输入数字喔</string>
      <string name="homepage_lable">首页</string>
      <string name="homepage_uri">http://www.baidu.com/</string>
</resources>

在修改“Uri.parse()”函数,传入资源标识符:

Uri uri = Uri.parse(R.string.homepage_uri);

但是系统提示在parse处出现了错误,因为“Uri.parse()”函数并不接受资源标识符类型的输入。

所以真正的“Uri.parse()”函数代码如下:

Uri uri = Uri.parse(getString(R.string.homepage_uri));



如何避免出错:

方法: 用try......catch语句来包住它:

public void onClick(DialogInterface dialoginterface, int i) {
			// TODO Auto-generated method stub
			try {
			Uri uri = Uri.parse(getString(R.string.homepage_uri));
			Intent intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);
			
		}catch(URISyntaxException e){
			
		}
	}


不过系统提示URISyntaxException有错误,错误为执行不到的 URISyntaxException 的 catch 块。从未从 try 语句主体抛出此异常,系统提示的修正方法为除去catch语句,或将catch语句替换为throw。

大家说怎么改呢?



以下是完整的“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.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
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.
	 * 
	 * @param <calcBMI>
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findViews();
		setListensers();
		
		// Listen for button clicks
		//Button button = (Button) findViewById(R.id.submit);
		//button.setOnClickListener(calcBMI);
	}
	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);
	}
	// Listen for button clicks
	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");
			try{
			//EditText fieldheight = (EditText) findViewById(R.id.height);
			//EditText fieldweight = (EditText) findViewById(R.id.weight);
			double height = Double
					.parseDouble(field_height.getText().toString()) / 100;
			double weight = Double
					.parseDouble(field_weight.getText().toString());
			double BMI = weight / (height * height);

			//TextView result = (TextView) findViewById(R.id.result);
			//result.setText("Your BMI is " + nf.format(BMI));
			//Present result
			view_result.setText(getText(R.string.bmi_result)+nf.format(BMI));
			// Give health advice
		//	TextView fieldsuggest = (TextView) findViewById(R.id.suggest);
			if (BMI > 25) {
				view_result.setText(R.string.advice_heavy);
			} else if (BMI < 20) {
				view_result.setText(R.string.advice_light);
			} else {
				view_result.setText(R.string.advice_average);
			}
			openOptionsDialog();
		}catch(Exception obj){
			Toast.makeText(Bmi.this, R.string.input_error, Toast.LENGTH_SHORT).show();
		}
	}
	};

private void openOptionsDialog(){
	//Toast.makeText(Bmi.this, "BMI 计算器", Toast.LENGTH_SHORT).show();
	new AlertDialog.Builder(Bmi.this)
	.setTitle(R.string.about_title)
	.setMessage(R.string.about_msg)
	.setPositiveButton(R.string.ok_lable,
			new DialogInterface.OnClickListener(){
		public void onClick(
			DialogInterface dialoginterface,int i){
		}
	})
	.setNegativeButton(R.string.homepage_lable, new DialogInterface.OnClickListener() {
		
		//@Override
		public void onClick(DialogInterface dialoginterface, int i) {
			// TODO Auto-generated method stub
			//try {
			Uri uri = Uri.parse(getString(R.string.homepage_uri));
			Intent intent = new Intent(Intent.ACTION_VIEW,uri);
			startActivity(intent);
			
		//}catch(URISyntaxException e){
			
		//}
	}
	})
	.show();
	
	}
}




运行结果:





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐