在布局里添加一些空行:

  <View
        android:layout_width="fill_parent"
        android:layout_height="30dp"> 
   </View>

—————————————————————————————————————

若需要在调用另外一个Activity的同时传递数据,那么就需要利用android.os.Bundle对象封装数据的能力,将欲传递的数据或参数,通过Bundle来传递不同Intent之间的数据。

本范例的设计为一个简易表单的范例,在Activity1中收集User输入的数据,在离开Activity1的同时,将User选择的结果传递至下一个Activity2,以一个简单BMI"标准体重计算器"示范如何传递数据到下一个Activity里。

运行结果

 

 

 

 

 

1.在第一个Activity1主程序中,定义了"性别"选项的RadioGroup以及输入身高的"EditText",并运用Intent及Bundle对象,在调用Activity2(EX03_10_1)时,同时将数据传入。关于EditText对象的使用在此仅供参考,详细的应用以及属性方法,将会在未来讨论控件时,再详细解说。

 

 

 

 

 
  
  1. package com.xc.ex03_10;  
  2.  
  3. import java.text.DecimalFormat;  
  4. import java.text.NumberFormat;  
  5.  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.TextView;  
  9.  
  10. public class EX03_10_1 extends Activity {  
  11.     /** Called when the activity is first created. */ 
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.mylayout);  
  16.         Bundle bundle = this.getIntent().getExtras();  
  17.         String sex = bundle.getString("sex");  
  18.         double height = bundle.getDouble("height");  
  19.         String sexText = "";  
  20.         if(sex.equals("M")){  
  21.             sexText = "男性";  
  22.         }  
  23.         else{  
  24.             sexText = "女性";  
  25.         }  
  26.         String weight = this.getWeight(sex,height);  
  27.         TextView tv1 = (TextView)findViewById(R.id.text1);  
  28.         tv1.setText("你是一位"+sexText+"\n你的身高是"                   
  29.                 +height+"厘米\n你的标准体重是"+weight+"公斤");  
  30.     }  
  31.  
  32.     /* 四舍五入的method */     
  33.     private String format(double num) {        
  34.         NumberFormat formatter = new DecimalFormat("0.00");        
  35.         String s=formatter.format(num);        
  36.         return s;      
  37.     }       
  38.     /* 以findViewById()取得Button对象,并添加onClickListener */        
  39.     private String getWeight(String sex,double height) {        
  40.         String weight="";        
  41.         if(sex.equals("M")) {          
  42.             weight=format((height-80)*0.7);        
  43.         }        
  44.         else {          
  45.             weight=format((height-70)*0.6);        
  46.         }          
  47.         return weight;      
  48.     }     
  49. }  

2.那么,在Activity2(EX03_10_1)要如何接收来自Activity1(EX03_10)传递来的数据呢?试想,在Activity1是以Bundle封装对象,自然在Activity2亦是以Bundle的方式解开封装的数据;程序中以getIntent().getExtras() 方法取得随着Bundle对象传递过来的性别与身高,经过计算之后,显示在屏幕上。

 

 
  
  1. package com.xc.ex03_10;  
  2.  
  3. import java.text.DecimalFormat;  
  4. import java.text.NumberFormat;  
  5.  
  6. import android.app.Activity;  
  7. import android.os.Bundle;  
  8. import android.widget.TextView;  
  9.  
  10. public class EX03_10_1 extends Activity {  
  11.     /** Called when the activity is first created. */ 
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.mylayout);  
  16.         Bundle bundle = this.getIntent().getExtras();  
  17.         String sex = bundle.getString("sex");  
  18.         double height = bundle.getDouble("height");  
  19.         String sexText = "";  
  20.         if(sex.equals("M")){  
  21.             sexText = "男性";  
  22.         }  
  23.         else{  
  24.             sexText = "女性";  
  25.         }  
  26.         String weight = this.getWeight(sex,height);  
  27.         TextView tv1 = (TextView)findViewById(R.id.text1);  
  28.         tv1.setText("你是一位"+sexText+"\n你的身高是"                   
  29.                 +height+"厘米\n你的标准体重是"+weight+"公斤");  
  30.     }  
  31.  
  32.     /* 四舍五入的method */     
  33.     private String format(double num) {        
  34.         NumberFormat formatter = new DecimalFormat("0.00");        
  35.         String s=formatter.format(num);        
  36.         return s;      
  37.     }       
  38.     /* 以findViewById()取得Button对象,并添加onClickListener */        
  39.     private String getWeight(String sex,double height) {        
  40.         String weight="";        
  41.         if(sex.equals("M")) {          
  42.             weight=format((height-80)*0.7);        
  43.         }        
  44.         else {          
  45.             weight=format((height-70)*0.6);        
  46.         }          
  47.         return weight;      
  48.     }     
  49. }  

3. mylayout.xml为(EX03_10_1)的Layout,定义了显示计算结果的TextView。

 

 
  
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <AbsoluteLayout      
  3.     android:layout_width="fill_parent"     
  4.     android:layout_height="fill_parent"     
  5.     xmlns:android="http://schemas.android.com/apk/res/android" >      
  6.     <TextView        
  7.         android:id="@+id/text1"       
  8.         android:layout_width="wrap_content"      
  9.         android:layout_height="wrap_content"       
  10.         android:textSize="20sp"       
  11.         android:layout_x="50px"       
  12.         android:layout_y="72px"   >      
  13.         </TextView>    
  14. </AbsoluteLayout>  

 4.main.xml为(EX03_10)的Layout.

 

 
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.  
  7.     <View 
  8.         android:layout_width="fill_parent" 
  9.         android:layout_height="30dp" > 
  10.     </View> 
  11.  
  12.     <TextView 
  13.         android:layout_width="fill_parent" 
  14.         android:layout_height="wrap_content" 
  15.         android:gravity="center" 
  16.         android:text="@string/hello" 
  17.         android:textSize="24sp" /> 
  18.  
  19.     <View 
  20.         android:layout_width="fill_parent" 
  21.         android:layout_height="30dp" > 
  22.     </View> 
  23.  
  24.  
  25.  
  26.     <LinearLayout 
  27.         android:layout_width="wrap_content" 
  28.         android:layout_height="wrap_content" 
  29.         android:layout_gravity="center" 
  30.         android:orientation="horizontal" > 
  31.  
  32.         <TextView 
  33.             android:layout_width="fill_parent" 
  34.             android:layout_height="wrap_content" 
  35.             android:text="@string/sex" 
  36.             android:textSize="20sp" /> 
  37.  
  38.         <RadioGroup 
  39.             android:orientation="horizontal" 
  40.             android:layout_width="wrap_content" 
  41.             android:layout_height="wrap_content" > 
  42.  
  43.                 <RadioButton 
  44.                     android:id="@+id/man" 
  45.                     android:layout_width="fill_parent" 
  46.                     android:layout_height="wrap_content" 
  47.                     android:text="@string/man" /> 
  48.  
  49.                 <RadioButton 
  50.                     android:id="@+id/woman" 
  51.                     android:layout_width="fill_parent" 
  52.                     android:layout_height="wrap_content" 
  53.                     android:text="@string/woman" /> 
  54.              
  55.         </RadioGroup> 
  56.     </LinearLayout> 
  57.  
  58.     <View 
  59.         android:layout_width="fill_parent" 
  60.         android:layout_height="10dp" > 
  61.     </View> 
  62.  
  63.     <LinearLayout 
  64.         android:layout_width="wrap_content" 
  65.         android:layout_height="wrap_content" 
  66.         android:layout_gravity="center" 
  67.         android:orientation="horizontal" > 
  68.  
  69.         <TextView 
  70.             android:layout_width="fill_parent" 
  71.             android:layout_height="wrap_content" 
  72.             android:text="@string/height" 
  73.             android:textSize="20sp" /> 
  74.  
  75.         <EditText 
  76.             android:id="@+id/height" 
  77.             android:layout_width="100dp" 
  78.             android:layout_height="wrap_content" /> 
  79.  
  80.         <TextView 
  81.             android:layout_width="fill_parent" 
  82.             android:layout_height="wrap_content" 
  83.             android:text="@string/cm" 
  84.             android:textSize="18sp" /> 
  85.     </LinearLayout> 
  86.     <View 
  87.         android:layout_width="fill_parent" 
  88.         android:layout_height="30dp"> 
  89.     </View> 
  90.     <Button 
  91.         android:id="@+id/button1" 
  92.         android:layout_width="wrap_content" 
  93.         android:layout_height="wrap_content" 
  94.         android:layout_gravity="center" 
  95.         android:text="@string/count" 
  96.         android:textSize="18sp" /> 
  97.  
  98. </LinearLayout> 

5.格式化输出NumberFormat

设定整数或小数部分所显示的最少和最多位数,可以使用NumberFormat类
的方法:
setMinimumIntegerDigits(int)设置整数部分至少多少位
setMinimumFractionDigits(int)
setMaximumIntegerDigits(int)设置小数点后面位数为5
setMaximumFractionDigits(int)

设定小数部分的最多位很有用处。如果小数部分丢失的第一位数字大于等于5,
那么显示的最后一位会增1(四舍五入)。如果要显示尾随的零,可以把小数部分的最少位等于最多位。
如果不想显示,可以把小数部分的最少位设定为0或不设定。


6.DecimalFormat

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。该类设计有各种功能,使其能够分析和格式化任意语言环境中的数,包括对西方语言、阿拉伯语和印度语数字的支持。它还支持不同类型的数,包括整数 (123)、定点数 (123.4)、科学记数法表示的数 (1.23E4)、百分数 (12%) 和金额 ($123)。所有这些内容都可以本地化。

DecimalFormat 包含一个模式 和一组符号

符号含义:

0 一个数字

# 一个数字,不包括 0

. 小数的分隔符的占位符

, 分组分隔符的占位符

; 分隔格式。

- 缺省负数前缀。

% 乘以 100 和作为百分比显示

? 乘以 1000 和作为千进制货币符显示;用货币符号代替;如果双写,用

国际货币符号代替。如果出现在一个模式中,用货币十进制分隔符代

替十进制分隔符。

X 前缀或后缀中使用的任何其它字符,用来引用前缀或后缀中的特殊字符。

例子:

DecimalFormat df1 = new DecimalFormat("0.0");

DecimalFormat df2 = new DecimalFormat("#.#");

DecimalFormat df3 = new DecimalFormat("000.000");

DecimalFormat df4 = new DecimalFormat("###.###");

System.out.println(df1.format(12.34));

System.out.println(df2.format(12.34));

System.out.println(df3.format(12.34));

System.out.println(df4.format(12.34));

结果:

12.3

12.3

012.340

12.34


DecimalFormat format = new DecimalFormat("###,####.000");

System.out.println(format.format(111111123456.1227222));


Locale.setDefault(Locale.US);

DecimalFormat usFormat = new DecimalFormat("###,###.000");

System.out.println(usFormat.format(111111123456.1227222));


DecimalFormat addPattenFormat = new DecimalFormat();

addPattenFormat.applyPattern("##,###.000");

System.out.println(addPattenFormat.format(111111123456.1227));


DecimalFormat zhiFormat = new DecimalFormat();

zhiFormat.applyPattern("0.000E0000");

System.out.println(zhiFormat.format(10000));

System.out.println(zhiFormat.format(12345678.345));


DecimalFormat percentFormat = new DecimalFormat();

percentFormat.applyPattern("#0.000%");

System.out.println(percentFormat.format(0.3052222));


结果

1111,1112,3456.123

111,111,123,456.123

111,111,123,456.123

1.000E0004

1.235E0007

30.522%


如果使用具有多个分组字符的模式,则最后一个分隔符和整数结尾之间的间隔才是使用的分组大小。所以 "#,##,###,####" == "######,####" == "##,####,####"。

 

不过在Android的SDK中没有找到DecimalFormat的format()方法,只有NumberFormat的format方法。