* 程序的版权和版本声明部分
* 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();
}
}
运行结果:
点击“确认”按钮后: