Android 页面跳转传递参数及页面返回接收参数

HelloWorldActivity.java

package syit.david;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HelloWorldActivity extends Activity
{
	private Button btn;
	public final static int REQUEST_CODE = 1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btn = (Button) findViewById(R.id.btn1);
		btn.setOnClickListener(listener);

	}

	private OnClickListener listener = new OnClickListener()
	{

		public void onClick(View v)
		{
			Intent intent = new Intent();
			intent.setClass(HelloWorldActivity.this, SecondActivity.class);
			intent.putExtra("str", "First Android Application!");
			//startActivity(intent);
			startActivityForResult(intent, REQUEST_CODE);
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(requestCode==REQUEST_CODE)
		{
			if(resultCode==SecondActivity.RESULT_CODE)
			{
				Bundle bundle = data.getExtras();
				String str = bundle.getString("back");
				Toast.makeText(HelloWorldActivity.this, str, Toast.LENGTH_LONG).show();
			}
		}
	}

	
}



SecondActivity.java

package syit.david;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SecondActivity extends Activity
{
	private TextView tv;
	private Button btn;
	public final static int RESULT_CODE = 1;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		tv = (TextView)findViewById(R.id.secondText);
		
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		String s = bundle.getString("str");
		tv.setText(s);
		
		btn = (Button)findViewById(R.id.btn2);
		btn.setOnClickListener(listener);
	}
	
	public OnClickListener listener = new OnClickListener()
	{
		public void onClick(View v)
		{
			Intent intent = new Intent();
			intent.putExtra("back", "Back Data");
			setResult(RESULT_CODE, intent);
			finish();
		}
	};
	
}




layout目录下的2个xml文件main和second

main.xml

<?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" >

    <TextView
        android:id="@+id/firstText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<Button 
	    android:id="@+id/btn1"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/btnText1"
	    />
</LinearLayout>

second.xml

<?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" >

    <TextView
        android:id="@+id/secondText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<Button 
	    android:id="@+id/btn2"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="@string/btnText2"
	    />
</LinearLayout>


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="syit.david"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWorldActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"
           	android:label="@string/app_name">
        </activity>
    </application>
	<uses-permission android:name="android.permission.CALL_PHONE"/>
	<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>

注意点:

1、新建一个Android的Activity后,都需要在AndroidManifest.xml文件中进行注册声明。


2、页面传递参数使用 startActivityForResult()方法,并实写其 onActivityResult()方法,在方法里进行判断处理。


3、接收页面使用setResult()方法,将值返回。


上一篇:Unity获取方法的参数和方法名称


下一篇:ASP.net用法系列:如何从基类调用LINQ/EF类的属性