读书人

Android开发-身高体重指数(BIM)计算

发布时间: 2012-08-31 12:55:03 作者: rapoo

Android开发--身高体重指数(BIM)计算--添加对话框—ialog)(Toast界面组件、错误处理try-catch)
/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:添加对话框—ialog)(Toast界面组件、错误处理)

* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 09 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:

* 程序头部的注释结束

*/

①Toast页面组件:

Toast页面组件的作用是弹出一个消息框,快速在屏幕上显示一小段信息。

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){}}).show();*/}


调用“show()”方法来将Taost组件显示在屏幕上

运行结果:

Android开发-身高体重指数(BIM)计算-添加对话框—ialog)(Toast界面组件、异常处理try-catch)

②错误处理:

前面使用XML说明文件定义界面时,在字段的属性中添加了只能输入数字的限制。现在我们将体重(weight)输入的限制移除,允许输入除了整数之外的其他符号(为了可以输入小数点位数的体重)。

打开“res/layout/main.xml”文件,把说明文件中的语句从原本的:

<EditText android:id="@+id/weight"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:numeric="integer"    android:text=""    />


改为:

<EditText android:id="@+id/weight"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:singleLine="true"    android:text=""    />


以上代码增加了“android:singleLine="true"”单行输入的属性。

注意:这样改有一个缺点:当用户在“体重”字段输入了非整数也非浮点数的值时,整个程序就崩溃了 。

因此在下面的程序改进中,使用“try......catch”语句做错误处理,利用“Toast”组件来通知用户他们的输入有误。

try......catch插入的位置如下:

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 resultview_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, "打错了吗?只能输入数字喔", Toast.LENGTH_SHORT).show();}};};


运行结果:

Android开发-身高体重指数(BIM)计算-添加对话框—ialog)(Toast界面组件、异常处理try-catch)

③提取字符串:

为了更好的重用,继续把字符串提取到“res/values/string.xml”中:

<?xml version="1.0" encoding="utf-8"?><resources>............<string name="input_error">打错了吗?只能输入数字喔</string></resources>


然后在程序中使用“R.string.inputerror”来取得字符串:

Toast.makeText(Bmi.this, "BMI 计算器", Toast.LENGTH_SHORT).show();


一下是修改后完整的“Bmi.java文件代码:

package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.res.ColorStateList;import android.content.res.Resources;import android.content.res.XmlResourceParser;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> */@Overridepublic 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 clicksprivate 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 resultview_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){}}).show();*/}}


运行结果:

Android开发-身高体重指数(BIM)计算-添加对话框—ialog)(Toast界面组件、异常处理try-catch)

读书人网 >Android

热点推荐