12- 多个Activity与参数传递

主界面

package cn.yza.other;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class OtherActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
//		Uri uri = Uri
//				.parse("content://com.yza.provider.personprovider/person/");
//		this.getContentResolver().registerContentObserver(uri, true,
//				new PersonContentObserver(new Handler()));
	}

	public void openNewWin(View v) {
		  Intent intent =  new Intent();
		 //方法1
		 //intent.setClass(this, TempActivity.class);//要激活的组件
		 //方法2 反射
		 // intent.setClassName(this, "cn.yza.other.TempActivity");
		 //方法3
		 //intent.setComponent(new ComponentName(this,  TempActivity.class));
		//方法4
		//Intent intent =  new Intent(this,  TempActivity.class);
		//方法5 用于激活其他应用
		  
		  //单个添加
 		  intent.setClassName("cn.yza.other", "cn.yza.other.TempActivity");
//		  intent.putExtra("company", "yza");
//		  intent.putExtra("age", 34);
		  
		  //批量添加
		  Bundle b = new Bundle();
		  b.putString("company", "yza");
		  b.putInt("age", 12);
		  intent.putExtras(b);
		 // startActivity(intent);
		  startActivityForResult(intent, 200);//添加请求码 控制返回时候的路径

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		String result = data.getStringExtra("result");
		Toast.makeText(this, result, 1).show();
	}
	
}


package cn.yza.other;

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

public class TempActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.temp);
		
 		Intent intent = getIntent();
 		//取值方法1
//		String com = intent.getStringExtra("company");
//		int age = intent.getIntExtra("age", 0);
 		//取值方法2
		Bundle b = intent.getExtras();
		String com =b.getString("company");
		int age =b.getInt("age");
		TextView tv = (TextView) this.findViewById(R.id.info);
		tv.setText("com" +com+"  age" + age);
	}
	
	public void closeActivity(View v){
		Intent intent = new Intent();
		intent.putExtra("result", "张晓晓");
		setResult(30, intent);
		this.finish();//关闭当前
	}
}

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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
	<Button android:text="新的窗口"
	    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
	    android:onClick="openNewWin"/>
</LinearLayout>

temp.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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="我是新的activity" 
        android:id="@+id/info" /> 
    <Button android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="关闭" 
        android:onClick="closeActivity"
        />
</LinearLayout>
AndroidManifest.xml

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:label="new窗口"
            android:name=".TempActivity"></activity>
            
            
        <uses-library android:name="android.test.runner" />
    </application>
 <instrumentation
        android:label="test my app"
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="cn.yza.other" />
</manifest>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值