/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:修改表达用户界面
* 作 者: 雷恒鑫
* 完成日期: 2012 年 08 月 08 日
* 版 本 号: V1.0
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:
* 程序头部的注释结束
*/
Activity 分为独立的Activity和相依赖的Activity。
下面是对独立的Activity所做的实验:
新增加的“report.java”程序为:
package com.demo.android.bmi;import java.text.DecimalFormat;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class Report extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.report); findViews(); showResults(); setListensers(); } private Button button_calc; private TextView view_result; private TextView view_suggest; private void findViews() { button_calc = (Button) findViewById(R.id.report_back); view_result = (TextView) findViewById(R.id.result); view_suggest = (TextView) findViewById(R.id.suggest); } // Listen for button clicks private void setListensers() { button_calc.setOnClickListener(backMain); } private Button.OnClickListener backMain = new Button.OnClickListener() { public void onClick(View v) { //Close this Activity Report.this.finish(); }};private void showResults(){ DecimalFormat nf = new DecimalFormat("0.00"); Bundle bunde = this.getIntent().getExtras(); double height = Double.parseDouble(bunde.getString("KEY_HEIGHT")) / 100; double weight = Double.parseDouble(bunde.getString("KEY_WEIGHT")); double BMI = weight / (height * height); // Present result view_result.setText(getString(R.string.bmi_result) + nf.format(BMI)); // Give health advice 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); }}}
修改后的“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.Menu;import android.view.MenuItem;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(); } 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) { //Switch to report page Intent intent = new Intent(); intent.setClass(Bmi.this, Report.class); Bundle bundle = new Bundle(); bundle.putString("KEY_HEIGHT", field_height.getText().toString()); bundle.putString("KEY_WEIGHT", field_weight.getText().toString()); intent.putExtras(bundle); startActivity(intent); } }; }
运行结果:
经验积累:
1.我学会了如何使用Intent传递数据。
2.我学会了使用Intent接收信息。