Intent用法

来源:互联网 发布:打击电信网络诈骗作文 编辑:程序博客网 时间:2024/06/06 03:32
用法一:跳转后的activity不需要回传参数
send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <Button         android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转到另一个Activity"/></LinearLayout>


receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >   <TextView        android:id="@+id/txt"       android:layout_width="fill_parent"       android:layout_height="wrap_content"/></LinearLayout>


Send.java
package com.example.intentdemo;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 Send extends Activity {private Button btn=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.send);btn=(Button)findViewById(R.id.btn);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(Send.this, Receive.class);intent.putExtra("param", "你好!");startActivity(intent);}});}}


Receive.java
package com.example.intentdemo;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class Receive extends Activity {private TextView txt=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.receive);txt=(TextView)findViewById(R.id.txt);Intent intent=getIntent();txt.setText(intent.getStringExtra("param"));}}


用法二:跳转后的activity需要回传参数



send.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    >     <TextView        android:id="@+id/txt"       android:layout_width="fill_parent"       android:layout_height="wrap_content"/>    <Button         android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转到Receive1"/>    <Button         android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="跳转到Receive2"/></LinearLayout>


receive.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    >   <TextView        android:id="@+id/txt"       android:layout_width="fill_parent"       android:layout_height="wrap_content"/>   <Button         android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="回到第一个Activity"/></LinearLayout>


Send.java
package com.example.intentdemo;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 Send extends Activity {private Button btn1=null;private Button btn2=null;private TextView txt=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.send);btn1=(Button)findViewById(R.id.btn1);btn2=(Button)findViewById(R.id.btn2);txt=(TextView)findViewById(R.id.txt);btn1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(Send.this, Receive1.class);intent.putExtra("param", "这是Receive1");startActivityForResult(intent, 1);}});btn2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(Send.this, Receive2.class);intent.putExtra("param", "这是Receive2");startActivityForResult(intent, 2);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if(resultCode==RESULT_OK){switch(requestCode){case 1:txt.setText("从Receive1返回数据"+data.getStringExtra("ret"));break;case 2:txt.setText("从Receive2返回数据"+data.getStringExtra("ret"));break;default:break;}}}}


Receive1.java
package com.example.intentdemo;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 Receive1 extends Activity {private TextView txt=null;private Button btn=null;private Intent intent=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.receive);txt=(TextView)findViewById(R.id.txt);btn=(Button)findViewById(R.id.btn);intent=getIntent();txt.setText(intent.getStringExtra("param"));btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {intent.putExtra("ret", "1");setResult(RESULT_OK, intent);finish();}});}}


Receive2.java
package com.example.intentdemo;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 Receive2 extends Activity {private TextView txt=null;private Button btn=null;private Intent intent=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.receive);txt=(TextView)findViewById(R.id.txt);btn=(Button)findViewById(R.id.btn);intent=getIntent();txt.setText(intent.getStringExtra("param"));btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {intent.putExtra("ret", "2");setResult(RESULT_OK, intent);finish();}});}}

用法二的结果: