在布局里添加一些空行:
<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对象的使用在此仅供参考,详细的应用以及属性方法,将会在未来讨论控件时,再详细解说。
- package com.xc.ex03_10;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class EX03_10_1 extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mylayout);
- Bundle bundle = this.getIntent().getExtras();
- String sex = bundle.getString("sex");
- double height = bundle.getDouble("height");
- String sexText = "";
- if(sex.equals("M")){
- sexText = "男性";
- }
- else{
- sexText = "女性";
- }
- String weight = this.getWeight(sex,height);
- TextView tv1 = (TextView)findViewById(R.id.text1);
- tv1.setText("你是一位"+sexText+"\n你的身高是"
- +height+"厘米\n你的标准体重是"+weight+"公斤");
- }
- /* 四舍五入的method */
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String s=formatter.format(num);
- return s;
- }
- /* 以findViewById()取得Button对象,并添加onClickListener */
- private 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;
- }
- }
2.那么,在Activity2(EX03_10_1)要如何接收来自Activity1(EX03_10)传递来的数据呢?试想,在Activity1是以Bundle封装对象,自然在Activity2亦是以Bundle的方式解开封装的数据;程序中以getIntent().getExtras() 方法取得随着Bundle对象传递过来的性别与身高,经过计算之后,显示在屏幕上。
- package com.xc.ex03_10;
- import java.text.DecimalFormat;
- import java.text.NumberFormat;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class EX03_10_1 extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.mylayout);
- Bundle bundle = this.getIntent().getExtras();
- String sex = bundle.getString("sex");
- double height = bundle.getDouble("height");
- String sexText = "";
- if(sex.equals("M")){
- sexText = "男性";
- }
- else{
- sexText = "女性";
- }
- String weight = this.getWeight(sex,height);
- TextView tv1 = (TextView)findViewById(R.id.text1);
- tv1.setText("你是一位"+sexText+"\n你的身高是"
- +height+"厘米\n你的标准体重是"+weight+"公斤");
- }
- /* 四舍五入的method */
- private String format(double num) {
- NumberFormat formatter = new DecimalFormat("0.00");
- String s=formatter.format(num);
- return s;
- }
- /* 以findViewById()取得Button对象,并添加onClickListener */
- private 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;
- }
- }
3. mylayout.xml为(EX03_10_1)的Layout,定义了显示计算结果的TextView。
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- xmlns:android="http://schemas.android.com/apk/res/android" >
- <TextView
- android:id="@+id/text1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20sp"
- android:layout_x="50px"
- android:layout_y="72px" >
- </TextView>
- </AbsoluteLayout>
4.main.xml为(EX03_10)的Layout.
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <View
- android:layout_width="fill_parent"
- android:layout_height="30dp" >
- </View>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="@string/hello"
- android:textSize="24sp" />
- <View
- android:layout_width="fill_parent"
- android:layout_height="30dp" >
- </View>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/sex"
- android:textSize="20sp" />
- <RadioGroup
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <RadioButton
- android:id="@+id/man"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/man" />
- <RadioButton
- android:id="@+id/woman"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/woman" />
- </RadioGroup>
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="10dp" >
- </View>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/height"
- android:textSize="20sp" />
- <EditText
- android:id="@+id/height"
- android:layout_width="100dp"
- android:layout_height="wrap_content" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/cm"
- android:textSize="18sp" />
- </LinearLayout>
- <View
- android:layout_width="fill_parent"
- android:layout_height="30dp">
- </View>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:text="@string/count"
- android:textSize="18sp" />
- </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方法。