Android 利用Intent 两界面互相传值
2019-07-23 本文已影响0人
Cedric_h
原文:https://blog.csdn.net/uyy203/article/details/52621599
大致思路
- A-->B,在B界面中做完某些操作以后,得到结果,返回给A,让A根据返回的数据,更新自己的界面
1.A-->B,startActivityForResult(请求码,Intent);Intent用来启动B,传值给B
2.在B中做完某些操作之后,得到结果:setResult(返回码,Intent);Intent用来传值给A
3.A得到数据,让A根据返回的数据,更新自己的界面:在A中重写onActivityResult(请求码,返回码,Intent)
其中,请求码来自于第1步,返回码来自于第2步,Intent来自于第2步
4.根据3中的请求码,返回码和Intent,共同来更新A中的UI信息
界面A
MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button click;
private EditText info;
private TextView come;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=(Button) findViewById(R.id.click);
info=(EditText) findViewById(R.id.info);
come=(TextView) findViewById(R.id.come);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("msg",info.getText().toString());
intent.setClass(MainActivity.this,ReceiveActivity.class);
// startActivity(intent);
startActivityForResult(intent,0);//跳转并发送请求码
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
come.setText(data.getExtras().get("back").toString());//获得返回信息,并刷新UI
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.poco.transfer.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/info"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/click"
android:layout_below="@id/info"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/come"
android:text="null"
android:layout_below="@id/click"
/>
</RelativeLayout>
界面B
ReceiveActivity.java
public class ReceiveActivity extends AppCompatActivity {
private TextView tv;
private String s;
private Button back;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive);
tv=(TextView) findViewById(R.id.tv);
back=(Button) findViewById(R.id.back);
s=getIntent().getStringExtra("msg");
tv.setText(s);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.putExtra("back","back->"+s);
setResult(1,intent);//发送返回码
finish();//关闭当前界面
}
});
}
}
activity_receive.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="cn.poco.transfer.ReceiveActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/tv"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back"
android:id="@+id/back"
android:layout_below="@id/tv"
/>
</RelativeLayout>