如果要在次页面上加上一个"回到上一页"的按钮,而非通过模拟器的回复键,且回上一页后又能保留之前输入的相关信息.
那么就必须使用StartActivityForResult()来唤起一个Activity.
Main.axml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/title"
android:layout_width="243px"
android:layout_height="29px"
android:text="@string/title"
android:textSize="24sp"
android:layout_x="36px"
android:layout_y="32px" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="37px"
android:text="@string/text1"
android:textSize="18sp"
android:layout_x="40px"
android:layout_y="156px" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="29px"
android:text="@string/text2"
android:textSize="18sp"
android:layout_x="40px"
android:layout_y="102px" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cm"
android:textSize="18sp"
android:layout_x="231px"
android:layout_y="157px" />
<Button
android:id="@+id/button1"
android:layout_width="70px"
android:layout_height="48px"
android:text="计算"
android:layout_x="130px"
android:layout_y="232px" />
<RadioGroup
android:id="@+id/sex"
android:layout_width="300px"
android:layout_height="100px"
android:layout_x="97px"
android:layout_y="98px"
android:orientation="horizontal"
android:checkedButton="@+id/sex1">
<RadioButton
android:id="@+id/sex1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男的" />
<RadioButton
android:id="@+id/sex2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女的" />
</RadioGroup>
<EditText
android:id="@+id/height"
android:layout_width="130px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="96px"
android:layout_y="142px"
android:numeric="decimal" />
</AbsoluteLayout>
myalyout.axml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<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" />
<Button
android:id="@+id/button1"
android:layout_width="100px"
android:layout_height="48px"
android:text="回上一頁"
android:layout_x="110px"
android:layout_y="180px" />
</AbsoluteLayout>
MainActivity.cs
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace Ex03_11
{
[Activity(Label = "Ex03_11", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private EditText et;
private RadioButton rb1;
private RadioButton rb2;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button b1 = (Button)FindViewById(Resource.Id.button1);
b1.Click += delegate {
/*取得输入的身高*/
et = (EditText) FindViewById(Resource.Id.height);
double height=Double.Parse(et.Text.ToString());
/*取得选择的性别*/
String sex="";
rb1 = (RadioButton) FindViewById(Resource.Id.sex1);
rb2 = (RadioButton) FindViewById(Resource.Id.sex2);
if(rb1.Checked)
{
sex="M";
}
else
{
sex="F";
}
/*new一个Intent对象,并指定class*/
Intent intent = new Intent();
//intent.setClass(EX03_11.this,EX03_11_1.class);
intent.SetClass(this, typeof(Activity1));
/*new一个Bundle对象,并将要传递的数据传入*/
Bundle bundle1 = new Bundle();
bundle1.PutDouble("height",height);
bundle1.PutString("sex",sex);
/*将Bundle对象assign给Intent*/
intent.PutExtras(bundle1);
/*调用Activity EX03_11_1*/
//startActivityForResult(intent,0); } });
StartActivityForResult(intent,0);
};
}
/* 重写 onActivityResult()*/
override protected void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
switch (resultCode)
{
case Result.Ok:
/* 取得数据,并显示于画面上 */
Bundle bunde = data.Extras;
String sex = bunde.GetString("sex");
double height = bunde.GetDouble("height");
et.SetText(""+height,TextView.BufferType.Normal);
if(sex.Equals("M"))
{
rb1.Checked =true ;
}
else
{
rb2.Checked=true ;
}
break;
default:
break;
}
}
}
}
Activity1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace Ex03_11
{
[Activity(Label = "Activity1")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
/* 加载main.xml Layout */
SetContentView(Resource.Layout.myalyout);
/* 取得Intent中的Bundle对象 */
Bundle bunde = this.Intent.Extras;
/* 取得Bundle对象中的数据 */
string sex = bunde.GetString("sex");
double height = bunde.GetDouble("height");
/*判断性别 */
String sexText = "";
if (sex.Equals("M"))
{
sexText = "男性";
}
else
{
sexText = "女性";
}
/* 取得标准体重 */
string weight = this.getWeight(sex, height);
/* 设定输出文字 */
TextView tv1 = (TextView)FindViewById(Resource.Id.text1);
tv1.SetText("你是一位" + sexText + "\n你的身高是" + height +
"公分\n你的标准体重是" + weight + "公斤", TextView.BufferType.Normal);
Button button = FindViewById<Button>(Resource.Id.button1);
button.Click += delegate {
/* 回传result回上一个activity */
this.SetResult(Result.Ok,this.Intent);
/* 关闭activity */
this.Finish();
};
}
private String getWeight(String sex, double height)
{
String weight = "";
if (sex.Equals("M"))
{
weight = ((height - 80) * 0.7).ToString();
}
else
{
weight = ((height - 70) * 0.6).ToString();
}
return weight;
}
}
}