我承认有点儿标题党了,呵呵。其实就只是一个Activity跳转页面然后接收值做处理的例子而已。
废话不多说,直接进入正题。
首先我们利用网友用JAVA编写的基于android的可视化GUI布局拖拉工具程序 --DroidDraw。(点击这里下载)布局以下界面:
此页面位于res/layout/main.xml。
制作接收值页面,页面如下:
此页面位于res/layout/mainlayout.xml,到此为止准备工作全部做好了,接下来正式进入 我们的代码实现功能。。
主页面代码位于src/cn.terry/BundleObject.java
代码如下:
代码 1 import android.app.Activity; 2 import android.app.AlertDialog; 3 import android.os.Bundle; 4 import android.view.View; 5 import android.view.View.OnClickListener; 6 import android.widget. * ; 7 import android.content.DialogInterface; 8 import android.content.Intent; 9 import android.graphics.Color; 10 public class BundleObject extends Activity { 11 private Button mButton; 12 private EditText mEditText; 13 private RadioGroup mRadioGroup; 14 private double height; 15 private String Sex; 16 /** Called when the activity is first created. */ 17 @Override 18 public void onCreate(Bundle savedInstanceState) { 19 super .onCreate(savedInstanceState); 20 setContentView(R.layout.main); 21 mButton = (Button)findViewById(R.id.confirm); 22 mButton.setOnClickListener( new OnClickListener() { 23 24 @Override 25 public void onClick(View v) { 26 // TODO Auto-generated method stub 27 mEditText = (EditText)findViewById(R.id.heigt); 28 if (mEditText.getText().toString().length() == 0 ) 29 { 30 /* AlertDialog builder = new AlertDialog.Builder(BundleObject.this).create(); 31 builder.setTitle("提示"); 32 builder.setMessage("請輸入您的身高!!"); 33 34 builder.show(); */ 35 36 new AlertDialog.Builder(BundleObject. this ) 37 .setMessage( " 请輸入您的身高 " ) 38 .setTitle( " 提示 " ) 39 .setNeutralButton( " 确定 " , new DialogInterface.OnClickListener() { 40 @Override 41 public void onClick(DialogInterface dialog, int which) { 42 // TODO Auto-generated method stub 43 mEditText.setHighlightColor(Color.RED); 44 } 45 }).create() 46 .show(); 47 return ; 48 } 49 mRadioGroup = (RadioGroup)findViewById(R.id.sex); 50 height = Double.parseDouble(mEditText.getText().toString()); 51 if (mRadioGroup.getCheckedRadioButtonId() == R.id.M) 52 { 53 Sex = " M " ; 54 } 55 else 56 { 57 Sex = " F " ; 58 } 59 Intent intent = new Intent(); 60 intent.setClass(BundleObject. this , Next. class ); 61 Bundle bun = new Bundle(); 62 bun.putDouble( " Height " , height); 63 bun.putString( " Sex " , Sex); 64 intent.putExtras(bun); 65 startActivity(intent); 66 BundleObject. this .finish(); 67 } 68 }); 69 } 70 }
在此有一点想让大家注意的是:弹出对话框的时候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对象传递过来的性别与身高,经过计算之后,显示在屏幕上。
废话到此为止,第二个页面的处理程序如下:
代码 1 import java.text.DecimalFormat; 2 import java.text.NumberFormat; 3 import android.content.Intent; 4 import android.app.Activity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget. * ; 9 public class Next extends Activity { 10 private TextView mTextView01; 11 private TextView mTextView02; 12 private Button mButton1; 13 private String Sex; 14 private double Height; 15 private String SextText; 16 public void onCreate(Bundle savedInstanceState) { 17 super .onCreate(savedInstanceState); 18 setContentView(R.layout.mainlayout); 19 Bundle bun = this .getIntent().getExtras(); 20 Sex = bun.getString( " Sex " ); 21 Height = bun.getDouble( " Height " ); 22 if (Sex.equals( " M " )) 23 { 24 SextText = " 男性 " ; 25 } 26 else 27 { 28 SextText = " 女性 " ; 29 } 30 // 取得標準體重 31 String Weight = getWeight(Sex, Height); 32 mTextView01 = (TextView)findViewById(R.id.TextView01); 33 mTextView02 = (TextView)findViewById(R.id.TextView02); 34 String result = " 您是一位 " + SextText + 35 " 您的身高为: " + Height + " cm " ; 36 String result2 = " 您的标准体重为: " + Weight + " 千克 " ; 37 mTextView01.setText(result); 38 mTextView02.setText(result2); 39 40 mButton1 = (Button)findViewById(R.id.Button03); 41 mButton1.setOnClickListener( new OnClickListener() { 42 43 @Override 44 public void onClick(View v) { 45 // TODO Auto-generated method stub 46 Intent intent = new Intent(); 47 intent.setClass(Next. this , BundleObject. class ); 48 startActivity(intent); 49 Next. this .finish(); 50 } 51 }); 52 } 53 54 55 56 // 四舍五入的方法 57 private String format( double num) 58 { 59 NumberFormat formatter = new DecimalFormat( " 0.00 " ); 60 String s = formatter.format(num); 61 return s; 62 } 63 // 取得體重 64 public String getWeight(String Sex, double height) 65 { 66 String Weight = "" ; 67 if (Sex.equals( " M " )) 68 { 69 Weight = format((height - 80 ) * 0.7 ); 70 } 71 else 72 { 73 Weight = format((height - 70 ) * 0.6 ); 74 } 75 return Weight; 76 } 77 }
有一点需要大家注意的是,如果要在新建的项目多新建一个处理类那必需在配置文件为他显示声明,程序才会运行通过,这里配置文件java的命名方式如下:
AndroidManifest.xml 相当于asp.net 的web.config
为了代码的完整性,我顺便把配置文件也贴出来,高手可以直接跳过
代码 1 < manifest xmlns:android = " http://schemas.android.com/apk/res/android " 2 package = " cn.terry " 3 android:versionCode = " 1 " 4 android:versionName = " 1.0 " > 5 < application android:icon = " @drawable/icon " android:label = " @string/app_name " > 6 < activity android:name = " .BundleObject " 7 android:label = " @string/app_name " > 8 < intent - filter > 9 < action android:name = " android.intent.action.MAIN " /> 10 < category android:name = " android.intent.category.LAUNCHER " /> 11 </ intent - filter > 12 </ activity > 13 14 < activity android:name = " .Next " ></ activity > 15 </ application > 16 17 18 </ manifest >
红色字体是新建类的名字。
至此程序大功告成
程序源码:点击下载/Files/TerryBlog/BundleObject.rar
如有不懂的可以加我QQ:285735942 或者Email:terryyhl@gmail.com
转载于:https://www.cnblogs.com/TerryBlog/archive/2010/05/15/1736427.html
相关资源:Android 课设 在线订餐项目源码(含apk)