/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:添加对话框(Dialog)(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组件显示在屏幕上
运行结果:
②错误处理:
前面使用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 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, "打错了吗?只能输入数字喔", Toast.LENGTH_SHORT).show();
}
};
};
运行结果:
③提取字符串:
为了更好的重用,继续把字符串提取到“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>
*/
@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){
}
})
.show();
*/
}
}
运行结果:
分享到:
相关推荐
迈入BIM 2.0时代 --- 工程建设行业BIM发展的新趋势
BMI指数计算器 输入身高体重计算BMI指数,根据中国人体质判断健康状况!
教育科研-学习工具-一种BIM管理教学用黑板.zip
行业分类-物理装置-一种水利水电工程BIM族库构建方法
行业文档-设计装置-一种BIM管理教学用黑板
从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf从-BIM-到-CIM.pdf
行业分类-物理装置-一种桥梁预制件BIM信息化管理系统.zip
xeokit-bim-viewer 是一个开放源代码2D / 3D BIM查看器,它在浏览器中运行,并从文件系统中加载模型。 查看器基于,并作为一部分捆绑在一起。 查看器由和开发,并集成在和更高版本中。 该查看器可以用作独立...
BIM体质指数计算器,使用IDEA软件、Java语言编写的程序。
用threejs展示三维bim模型
Android实现体重指数BMI的计算,为初学者提供简单样例
ejercicios-clase-03-2bim 通用技术手册 通用许可文件普遍使用权的决定普遍存在的权责发生制和普遍存在的权衡。 普通民众的报酬 通用许可文件普遍使用权的决定普遍存在的权责发生制和普遍存在的权衡。 普通报刊...
基于SAP-ERP与建筑信息模型(BIM)系统大数据应用.ppt基于SAP-ERP与建筑信息模型(BIM)系统大数据应用.ppt基于SAP-ERP与建筑信息模型(BIM)系统大数据应用.ppt基于SAP-ERP与建筑信息模型(BIM)系统大数据应用.ppt基于SAP-...
基于传感器技术、物联网通信技术、组态监控软件、管廊平台测控终端RTU等,采集供水管网、供暖管网、蒸汽管网、压缩气管网介质的压力、温度、流量等参数,实现对管网的实时监测。利用应用云平台、移动网络架构,让...
BIM、图学会考试、1-14期pdf考试试题BIM、图学会考试、1-14期pdf考试试题BIM、图学会考试、1-14期pdf考试试题
智慧方案
BIM360 Design Guide for Revit users
行业文档-设计装置-基于BIM的钢结构建筑定位装置.zip
行业文档-设计装置-基于BIM的钢结构建筑定位贴片.zip