Android开发--身高体重指数(BIM)计算--添加对话框(Dialog)(设计对话框、定义调用点、实体对话框、重构、添加按钮)...

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:添加对话框(Dialog)

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

* 程序输出:

* 程序头部的注释结束

*/

①设计对话框:

要让BMI应用程序出现常见的“关于”页面,BMI应用程序的“关于”页面中常要包含版本信息、作者、联系方式。首页等信息。

要在Android程序中调用一个对话框,有两个主要步骤:1.定义调用点。2.实体对话框。

②定义调用点:

修改“Bmi.java”文件:

	public void onClick(View v) {
                ............
} else {
				fieldsuggest.setText(R.string.advice_average);
			}
			openOptionsDialog();
		}


解析:在“calcBMI”函数的末尾添加一行“openOptionsDialog();”,用以在每次计算完BMI值并显示建议后,顺便调用“关于”对话框。

③实体对话框:

紧接着在“calcBMI”函数这个“openOptionsDialog();”函数之后,开始编写对话框函数:

private void openOptionsDialog(){
	new AlertDialog.Builder(Bmi.this)
	.setTitle("关于 Android BMI")
	.setMessage("Android BMI Calc")
	.show();
}

④重构:

我们把其中用到的字符串提取出来,整理到“res/values/strings.xml”

“res/values/strings.xml”文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
...............
 <string name="about_title">关于 Android BMI</string>
    <string name="about_msg">Android BMI Calc</string>
............
</resources>


现在就可以使用资源文件来代替字符串了。于是“openOptionsDialog”函数变成这样:

private void openOptionsDialog(){
	new AlertDialog.Builder(Bmi.this)
	.setTitle(R.string.about_title)
	.setMessage(R.string.about_msg)
	.show();
}


打开模拟器,按下按钮后,运行结果:

⑤添加按钮:

为对话框添加一个确认按钮:

private void openOptionsDialog(){
	new AlertDialog.Builder(Bmi.this)
	.setTitle(R.string.about_title)
	.setMessage(R.string.about_msg)
	.setPositiveButton("确认",
			new DialogInterface.OnClickListener(){
		public void onClick(
			DialogInterface dialoginterface,int i){
		}
	})
	.show();
}
}

注意:要使用“DialogInterface”函数,需要先导入必须的库函数:

import android.content.DialogInterface;

重构:然后把其中用到的字符串提取出来,整理到“res/values/strings.xml”中。

“res/values/strings.xml”文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
............
 <string name="ok_lable">确认</string>
</resources>


下面是修改后完整的“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;

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 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("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();
		}
	};



private void openOptionsDialog(){
	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();
}
}


运行结果:

点击“确认”按钮后:

全志R58的官方开发板加载bmi160驱动的步骤3B.txt 开发板:全志R58的官方开发板R58_PER3_LPDDR3_32X1_V1_1.pdf(板载加速度传感器bma250) 目标:外挂bmi160模块可以检测到加速度和角速度(acc+gyr/加速度传感器+陀螺仪) BSP:r58_20160823.tar.gz(2016/8/22从全志的git服务器拿下来的系统) 显示:HDMI输出1080p分辨率的LCD显示器。 计划步骤: 1、打通开发板上的bma250(证明开发板硬件是好的。全志官方的BSP也是好的。) 2、将驱动程序bma250.c中的bma250全部替换为bmi160,验证是可以加入新的gsensor的(陀螺仪类似)。 3、借用bma250.c这个驱动程序,初始化的部分修改为初始化bmi160,调通BMI160的gsensor部分。 4、完善全志/博世提供的bmi160的驱动程序,调通BMI160的gsensor部分。 (陀螺仪部分鱼刺类似,陀螺仪部分借用l3gd20.c来验证bmi160的gyr部分) 下面进行第三步:借用bma250.c这个驱动程序,初始化的部分修改为初始化bmi160,调通BMI160的gsensor部分。 为了方便观察,直接注释掉除了bma250之外的全部的gsensor: Z:\home\wwt\only_bma250_r58\android\device\softwinner\common\hardware-common\libhardware\libsensors\aw_sensors\sensorDetect.cpp struct sensor_extend_t gsensorList[] = { { { "bma250", LSG_BMA250, }, { "Bosch 3-axis Accelerometer", "Bosch Sensortec", 1, 0, SENSOR_TYPE_ACCELEROMETER, 4.0f*9.81f, (4.0f*9.81f)/1024.0f, 0.2f, 0,0,0, { }, }, }, }; Z:\home\wwt\only_bma250_r58\android\device\softwinner\octopus-perf\configs\gsensor.cfg ;Direction parameter adjustment, including the x, y, z axis, and xy interchange four variables, ;the name of the module used for identification, and drive registered name consistent ;-------------------------- ;name:bma250 ;-------------------------- gsensor_name = bma250 gsensor_direct_x = false gsensor_direct_y = true gsensor_direct_z = true gsensor_xy_revert = true Z:\home\wwt\only_bma250_r58\android\device\softwinner\octopus-perf\BoardConfig.mk #gsensor & Gyr sensor SW_BOARD_USES_SENSORS_TYPE = aw_sensors 注意:lunch的f1选项在HAL层中使用的ST的9轴(ACC+GYR+MAG)传感器。 #gsensor & Gyr sensor SW_BOARD_USES_SENSORS_TYPE = lsm9ds0 Z:\home\wwt\only_bma250_r58\android\device\softwinner\octopus-perf\init.sun8i.rc on boot # use automatic det
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值