Intent用法

[b][size=x-large]用法一:跳转后的activity不需要回传参数[/size][/b]
[size=large]send.xml[/size]

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



[size=large]receive.xml[/size]

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


[size=large]Send.java[/size]

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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
btn=(Button)findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive.class);
intent.putExtra("param", "你好!");
startActivity(intent);
}
});
}
}


[size=large]Receive.java[/size]
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;
@Override
protected 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"));
}
}


[b][size=x-large]用法二:跳转后的activity需要回传参数[/size][/b]

[img]http://dl2.iteye.com/upload/attachment/0098/8546/1aa5cc19-9358-39d0-80c5-e7d3bbe57cc7.png[/img]

[size=large]send.xml[/size]

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


[size=large]receive.xml[/size]

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


[size=large]Send.java[/size]

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;
@Override
protected 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() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive1.class);
intent.putExtra("param", "这是Receive1");
startActivityForResult(intent, 1);
}
});

btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Send.this, Receive2.class);
intent.putExtra("param", "这是Receive2");
startActivityForResult(intent, 2);
}
});


}
@Override
protected 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;
}

}
}
}


[size=large]Receive1.java[/size]
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;
@Override
protected 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() {
@Override
public void onClick(View v) {
intent.putExtra("ret", "1");
setResult(RESULT_OK, intent);
finish();
}
});
}
}


[size=large]Receive2.java[/size]

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;
@Override
protected 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() {
@Override
public void onClick(View v) {
intent.putExtra("ret", "2");
setResult(RESULT_OK, intent);
finish();
}
});
}
}

[size=large]用法二的结果:[/size]

[img]http://dl2.iteye.com/upload/attachment/0098/8548/555ae992-e119-3e6b-a27a-fe879daf1469.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0098/8550/1d810288-3791-323c-9136-c312db6bbe19.png[/img]

[img]http://dl2.iteye.com/upload/attachment/0098/8552/0d119ebc-0ec6-3fb9-909d-7ef5c0dd4e9b.png[/img]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值