[b][size=x-large]用法一:跳转后的activity不需要回传参数[/size][/b]
[size=large]send.xml[/size]
[size=large]receive.xml[/size]
[size=large]Send.java[/size]
[size=large]Receive.java[/size]
[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]
[size=large]receive.xml[/size]
[size=large]Send.java[/size]
[size=large]Receive1.java[/size]
[size=large]Receive2.java[/size]
[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]
[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]
500

被折叠的 条评论
为什么被折叠?



