android(多个Activity之间切换)

一、切换Activity的5种方式

Intent intent = new Intent();
(1)intent.setClass(this,OtherActivity.class);
(2)intent.setClassName(this,"com.xiazdong.OtherActivity");
(3)intent.setClassName("com.xiazdong","com.xiazdong.OtherActivity");//此种方式用来激活不同应用的Activity,只需要指定第一个参数:包名 为另一个应用即可;
(4)
Component comp = new Component(this,OtherActivity.class);
intent.setComponent(comp);
(5)Intent intent = new Intent(this,OtherActivity.class);

二、发送参数与接收参数方式

1、putExtra方式:

发送
intent.putExtra("name","xiazdong");
intent.putExtra("age",20);
接收
String name = intent.getStringExtra("name");
int age = intent.getIntExtra("age");

2、Bundle方式:

发送
Bundle bundle = new Bundle();
bundle.putString("name","xiazdong");
bundle.putInt("age",20);
intent.putExtras(bundle);

接收
Bundle bundle = intent.getExtras();
String name = bundle.getString("name");
int age = bundle.getInt("age");

三、示例

3.1、没有返回值的跳转

第一个activity,跳转到下一个activity

package org.xiazdong;
 
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;
 
public class IntentActivity extends Activity {
	private Button btn1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1 = (Button)this.findViewById(R.id.brn1);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(IntentActivity.this, SubActivity.class);  //从IntentActivity跳转到SubActivity
				intent.putExtra("name", "xiazdong");  //放入数据
				startActivity(intent);  //开始跳转
			}
		});
    }
}

接受上一个activity传过来的参数

package org.xiazdong;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class SubActivity extends Activity{
	private TextView tv1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.sub);
		tv1 = (TextView)this.findViewById(R.id.tv1);
		Intent intent = this.getIntent();    //获得当前的Intent
 		Bundle bundle = intent.getExtras();  //获得全部数据
		String value = bundle.getString("name");  //获得名为name的值
		tv1.setText(value);
	}
 
}

3.2、有返回值的跳转示例

package org.xiazdong;
 
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 IntentActivity extends Activity {
	private Button btn1;
	private TextView tv1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn1 = (Button)this.findViewById(R.id.brn1);
        tv1 = (TextView)this.findViewById(R.id.tv2);
        btn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(IntentActivity.this, SubActivity.class);
				startActivityForResult(intent, 100); //requestcode=100
				
			}
		});
    }
    //回调函数,会根据requesrCode进行不同的响应
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==100&&resultCode==200){
			Bundle bundle = data.getExtras();
			String response = bundle.getString("response");
			tv1.setText(response);
		}
	}
    
}

package org.xiazdong;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class SubActivity extends Activity{
	private TextView tv1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.sub);
		tv1 = (TextView)this.findViewById(R.id.tv1);
		Intent intent = new Intent();  //创建一个Intent
		intent.putExtra("response", "来自2");
		setResult(200,intent);  //返回码为200
		finish();
	}
 
}

你可能感兴趣的:(android语言)