<?xml version="1.0" encoding =" utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation= "vertical "
android:layout_width= "fill_parent "
android:layout_height= "fill_parent ">
<TextView
android:id="@+id/tv"
android:layout_width= "fill_parent "
android:layout_height="wrap_content"
android:text="小晕,不醉。 先生:"
/>
<Button
android:id= "@+id/IntentBtn "
android:layout_width= "fill_parent "
android:layout_height= "wrap_content "
android:text= "我说上联 "
/>
</LinearLayout>
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 App1
{
[Activity(Label = "HelloIntent",MainLauncher= true ,@Icon = "@drawable/Icon")]
public class HelloIntent : Activity
{
protected override void OnCreate( Bundle bundle)
{
base.OnCreate(bundle);
SetContentView( Resource. Layout.HelloIntentLayout);
Button button = FindViewById< Button>( Resource. Id.IntentBtn);
button.Click += button_Click;
}
void button_Click( object sender, EventArgs e)
{
#region 简单意图到另外一个界面去
//Intent it = new Intent();
//it.SetClass(this, typeof(HelloIntent2));
//StartActivity(it);
#endregion
string sendValue = "山有木兮木有枝" ;
Toast.MakeText( this, sendValue, ToastLength.Short).Show();
#region 显示传值
Intent it = new Intent();
it.SetClass( this, typeof( HelloIntent2));
it.PutExtra( "testName", sendValue);
//此为普通调用。
// StartActivity(it);
//当有返回值时用到,前一个参数是intent的对象,第一个是OnActivityResult方法里的requestCode。
StartActivityForResult(it, 0);
#endregion
}
protected override void OnActivityResult( int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode==0)
{
if ( Result.Ok==resultCode)
{
string name = data.GetStringExtra( "nameExtra");
Toast.MakeText( this, name, ToastLength.Short).Show();
}
}
}
}
}
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 App1
{
[Activity(Label = "HelloIntent2")]
public class HelloIntent2 : Activity
{
protected override void OnCreate( Bundle bundle)
{
base.OnCreate(bundle);
SetContentView( Resource. Layout.HelloIntentLayout2);
#region 显示传递参数,这边要得到传递过来的值。
string uName = Intent.GetStringExtra( "testName");
Toast.MakeText( this, uName, ToastLength.Long).Show();
#endregion
Button btnClose = FindViewById< Button>( Resource. Id.btnClose);
btnClose.Click += btnClose_Click;
}
void btnClose_Click( object sender, EventArgs e)
{
string sendValue = "心悦君兮君不知" ;
Toast.MakeText( this, sendValue, ToastLength.Short).Show();
Intent it = new Intent();
it.PutExtra( "nameExtra", sendValue);
SetResult( Result.Ok,it);
Finish();
}
}
}