Android入门:Intent入门(2)

 

一、Intent与Activity应用

 

0.创建新的Activity步骤

 

在应用中创建新的Acitivity很常见;

步骤1:创建一个class继承Activity

 

步骤2:在Android-manifest.xml中添加<activity>元素

 

比如创建了一个名为SubActivity的Activity,则需要声明如下:

        <activity android:name=".SubActivity"></activity>


 

1.最简单的跳转到新的Activity

程序描述:MainActivity点击按钮后跳转到SubActivity,MainActivity传递一个(name,xiazdong)给SubActivity,并获得显示;

 

效果如下:

Android入门:Intent入门(2)

 

点击按钮后:

Android入门:Intent入门(2)

 

IntentActivity.java

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);  //开始跳转

			}

		});

    }

}


SubActivity.java

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);

	}



}


 

2.带返回值的跳转

 

跳转到新的Activity并在结束后将返回值传给原Activity

程序描述:MainActivity跳转到SubActivity后,SubActivity执行完后返回一个结果码给IntentActivity,执行对应的过程;

程序效果:

Android入门:Intent入门(2)

点击跳转后执行完第二个Activity后再跳回第一个Activity

Android入门:Intent入门(2)

 

IntentActivity.java

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

				

			}

		});

    }

	@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);

		}

	}

    

}

 

SubActivity.java

 

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)