Android 小項目之标准体重测量仪:Bundle对象的实现
制作接收值页面,页面如下:
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.*;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.graphics.Color;
- public class BundleObject extends Activity {
- private Button mButton;
- private EditText mEditText;
- private RadioGroup mRadioGroup;
- private double height;
- private String Sex;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mButton=(Button)findViewById(R.id.confirm);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mEditText=(EditText)findViewById(R.id.heigt);
- if(mEditText.getText().toString().length()==0)
- {
- /* AlertDialog builder = new AlertDialog.Builder(BundleObject.this).create();
- builder.setTitle(“提示”);
- builder.setMessage(“請輸入您的身高!!”);
- builder.show();*/
- new AlertDialog.Builder(BundleObject.this)
- .setMessage(“请輸入您的身高”)
- .setTitle(“提示”)
- .setNeutralButton(“确定”,new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- mEditText.setHighlightColor(Color.RED);
- }
- }).create()
- .show();
- return;
- }
- mRadioGroup=(RadioGroup)findViewById(R.id.sex);
- height=Double.parseDouble(mEditText.getText().toString());
- if(mRadioGroup.getCheckedRadioButtonId()==R.id.M)
- {
- Sex=”M”;
- }
- else
- {
- Sex=”F”;
- }
- Intent intent=new Intent();
- intent.setClass(BundleObject.this, Next.class);
- Bundle bun=new Bundle();
- bun.putDouble(“Height”, height);
- bun.putString(“Sex”, Sex);
- intent.putExtras(bun);
- startActivity(intent);
- BundleObject.this.finish();
- }
- });
- }
- }
在此有一点想让大家注意的是:弹出对话框的时候AlertDialog.Builder()这个方法在1.5以上都要加上类名.this 比如 我的页面的名字叫BundleObject.java就必须如下写下
- AlertDialog.Builder(BundleObject.this)
复制代码
1.5以下的版本直接this即可。。 到此为止己经完成了一半的功能了,那么在Acitivity2(Next.java)要如何接收来自Activity1(BundleObject.java)传递过来的数据呢?试想,在Activity1是以Bundle封装对象,自然在Activity2亦是以Bundle的方式来解开封装的数据咯;程序中以
- Bundle bun=this.getIntent().getExtras();
这样的方法来取得Bundle对象传递过来的性别与身高,经过计算之后,显示在屏幕上。废话到此为止,第二个页面的处理程序如下:
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.content.Intent;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.*;
- public class Next extends Activity {
- private TextView mTextView01;
- private TextView mTextView02;
- private Button mButton1;
- private String Sex;
- private double Height;
- private String SextText;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mainlayout);
- Bundle bun=this.getIntent().getExtras();
- Sex=bun.getString(“Sex”);
- Height=bun.getDouble(“Height”);
- if(Sex.equals(“M”))
- {
- SextText=”男性”;
- }
- else
- {
- SextText=”女性”;
- }
- //取得標準體重
- String Weight=getWeight(Sex, Height);
- mTextView01=(TextView)findViewById(R.id.TextView01);
- mTextView02=(TextView)findViewById(R.id.TextView02);
- String result = “您是一位” + SextText +
- “您的身高为:” + Height + “cm”;
- String result2= “您的标准体重为:” + Weight +”千克”;
- mTextView01.setText(result);
- mTextView02.setText(result2);
- mButton1=(Button)findViewById(R.id.Button03);
- mButton1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent();
- intent.setClass(Next.this, BundleObject.class);
- startActivity(intent);
- Next.this.finish();
- }
- });
- }
- //四舍五入的方法
- private String format(double num)
- {
- NumberFormat formatter=new DecimalFormat(“0.00”);
- String s=formatter.format(num);
- return s;
- }
- //取得體重
- public String getWeight(String Sex,double height)
- {
- String Weight=””;
- if(Sex.equals(“M”))
- {
- Weight=format((height-80)*0.7);
- }
- else
- {
- Weight=format((height-70)*0.6);
- }
- return Weight;
- }
- }
有一点需要大家注意的是,如果要在新建的项目多新建一个处理类那必需在配置文件为他显示声明,程序才会运行通过,这里配置文件java的命名方式如下:AndroidManifest.xml 相当于asp.net 的web.config为了代码的完整性,我顺便把配置文件也贴出来,高手可以直接跳过
- <manifest xmlns:android=”http://schemas.android.com/apk/res/android”
- package=”cn.terry”
- android:versionCode=”1″
- android:versionName=”1.0″>
- <application android:icon=”@drawable/icon” android:label=”@string/app_name”>
- <activity android:name=”.BundleObject”
- android:label=”@string/app_name”>
- <intent-filter>
- <action android:name=”android.intent.action.MAIN” />
- <category android:name=”android.intent.category.LAUNCHER” />
- </intent-filter>
- </activity>
- <activity android:name=”.Next”></activity>
- </application>
- </manifest>