Android 頁面跳轉傳遞引數及頁面返回接收引數

暖楓無敵發表於2012-06-02

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()方法,將值返回。


相關文章