安卓Activity之间的数据传递四种⽅法,以及数据回传的实现显式Intent来跳转页⾯和传递数据:
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("显式数据","显⽰意图Intent传送数据");
startActivity(intent);
接受数据时:
TextView txtshow=(TextView)findViewById(Show);
Intent intent =getIntent();
txtshow.StringExtra("显式数据"));
System.out.StringExtra("显式数据"));
隐式Intent
来执⾏⼀个动作,这个动作可以是系统⾃⼰定义的,⽐如发短信,打电话等等,action可以在l中定义
<activity android:name=".ReceiverActivity">
<intent-filter>
<action android:name="ReceiveData"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
使⽤时:
Intent intent =new Intent("ReceiveData");//为Intent添加动作
intent.addCategory("android.intent.category.DEFAULT");
//Category定义当前动作(Action)被执⾏的环境,如果不符合这个环境将⽆法激活,如,CATEGORY_DEFAULT
,默认环境,所有intent都能激活
startActivity(intent);//很奇怪,只会跳转到某个没有控件的界⾯
Bundle作为媒介:
Bundle bundle=new Bundle();
Intent intent =new Intent(MainActivity.this,ReceiverActivity.class);
bundle.putString("Bundle","Bundle传输数据");
intent.putExtra("Message",bundle);
startActivity(intent);
Serializable:
我们要创建⼀个类,并且让这个类(执⾏)implements Serializable
如:
ample.sendtypes;
import java.io.Serializable;
public class Serial implements Serializable {
public int getUserno() {
return userno;
}
public void setUserno(int userno) {
this.userno = userno;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
private int userno;
private String username;
}
接受数据代码:
Bundle bundle=getIntent().getBundleExtra("Message");  Bundle的代码和序列化的代码不可以同时运⾏,所以运⾏哪⼀个就把哪⼀个给注释
txtshow.String("Bundle"));
使⽤时:
Serial serial=new Serial();
serial.setUsername(“张三”);
serial.setUserno(001);
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra(“序列化”,serial);
startActivity(intent);
接受数据时:
Serial serial=(Serial)getIntent().getSerializableExtra("序列化");
txtshow.setText("序列化显⽰:\n ⽤户名:"+Username());
数据回传的实现
数据回传指的是,你可以在活动⼀中⽤intent传输数据给活动⼆,并且定义⼀个请求码,这个请求码是区分哪个到底是哪个活动发起的数据回调,因为很多个活动都要求数据回调时,系统会分不清数据到底是给哪⼀个,我们在活动⼆⾥⾯也要定义⼀个结果码,它是为了区分到底是哪⼀个回调数据,因为
活动⼆可能会回调很多个数据给活动⼀,如果没有结果码,那么活动⼀⽆法区分到底结果。(注意,发起回调的活动同时可以给返回结果的活动传输数值)
下⾯给代码:
请求回传的活动:
Intent intent =new Intent(MainActivity.this,ReturnActivity.class);//实现页⾯跳转
Bundle bundle=new Bundle();//定义⼀个Bundle装⼊数值
bundle.putString("name","⼩洪⾔");
bundle.putInt("age",20);
intent.putExtra("grilfriend",bundle);//将Bundle装⼊intent⾥⾯
startActivityForResult(intent,1);//要想得到数据回传就i必须要⽤startActivityForResult
第⼀个参数是我们定义的intent,第⼆个参数是请求码(request code)
回传数据的活动:
TextView txtshow2=(TextView)findViewById(Show2);
Intent intent=getIntent();
txtshow2.setText("姓名:"+BundleExtra("grilfriend").getString("name")+"\n年龄:"+BundleExtra("grilfriend").getInt("age"));        intent.putExtra("boyfriend","何佳洋");
setResult(1,intent);//第⼀个参数是结果码,区分到底是哪⼀个
然后第三步,我们要在发起请求数据回调的活动写⼀个⽅法,来操作返回的数据:
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode==1)
{
if(resultCode==1)
{
TextView txtshow=(TextView)findViewById(Return);
String s;
StringExtra("boyfriend");
System.out.println(s);
txtshow.setText(s);
}
}
}
下⾯给出总代码:
MainActivity.java:
ample.sendtypes;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
Button btnintentxian=(Button)findViewById(R.id.btnIntentxian);
Button btnintentyin=(Button)findViewById(R.id.btnIntentyin);
Button btnbundle=(Button)findViewById(R.id.btnBundle);
Button btnserializable=(Button)findViewById(R.id.btnSerializable);
Button btnreturn=(Button)findViewById(R.id.btnReturn);
btnintentxian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("显式数据","显⽰意图Intent传送数据");
startActivity(intent);
}
});
btnintentyin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent("ReceiveData");//为Intent添加动作
intent.addCategory("android.intent.category.DEFAULT");
//Category定义当前动作(Action)被执⾏的环境,如果不符合这个环境将⽆法激活,如,CATEGORY_DEFAULT ,默认环境,所有intent都能激活                startActivity(intent);//很奇怪,只会跳转到某个没有控件的界⾯
}
});
安卓intent用法
btnbundle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle bundle=new Bundle();
Intent intent =new Intent(MainActivity.this,ReceiverActivity.class);
bundle.putString("Bundle","Bundle传输数据");
intent.putExtra("Message",bundle);
startActivity(intent);
}
});
btnserializable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Serial serial=new Serial();
serial.setUsername("张三");
serial.setUserno(001);
Intent intent=new Intent(MainActivity.this,ReceiverActivity.class);
intent.putExtra("序列化",serial);
startActivity(intent);
}
});
btnreturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(MainActivity.this,ReturnActivity.class);
Bundle bundle=new Bundle();
bundle.putString("name","⼩洪⾔");
bundle.putInt("age",20);
intent.putExtra("grilfriend",bundle);
startActivityForResult(intent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if(requestCode==1)
{
if(resultCode==1)
{
TextView txtshow=(TextView)findViewById(Return);
String s;
StringExtra("boyfriend");
System.out.println(s);
txtshow.setText(s);
}
}
}
}
ReceiverActivity.java:
ample.sendtypes;
import androidx.appcompat.app.AppCompatActivity;
t.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ReceiverActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_receiver);
TextView txtshow=(TextView)findViewById(Show);
// Intent intent =getIntent();
// txtshow.StringExtra(“显式数据”));
/
/ System.out.StringExtra(“显式数据”));
//Bundle///
// Bundle bundle=getIntent().getBundleExtra(“Message”); Bundle的代码和序列化的代码不可以同时运⾏,所以运⾏哪⼀个就把哪⼀个给注释
// txtshow.String(“Bundle”));
Serializable//
// Serial serial=(Serial)getIntent().getSerializableExtra(“序列化”);
// txtshow.setText(“序列化显⽰:\n ⽤户名:”+Username());
}
}
ReturnActivity.java:
ample.sendtypes;
import androidx.appcompat.app.AppCompatActivity;
t.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ReturnActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_return);
TextView txtshow2=(TextView)findViewById(Show2);
Intent intent=getIntent();
txtshow2.setText("姓名:"+BundleExtra("grilfriend").getString("name")+"\n年龄:"+BundleExtra("grilfriend").getInt("age"));
intent.putExtra("boyfriend","何佳洋");
setResult(1,intent);
}
}
Serial.java:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。