(一)
1.效果图:点击按钮“调用第二个Activity”,转到第二页面,之后点击“返回数据”,将第二个页面的数据传到第一个页面
2.
activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.app4.MainActivity" 11 android:orientation="vertical"> 12 13 <Button 14 android:id="@+id/btn_start" 15 android:text=" 调用第二个Activity" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" /> 18 <TextView 19 android:id="@+id/tv_show" 20 android:textSize="40dp" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 /> 24 </LinearLayout>
slayout.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 <Button 7 android:id="@+id/btn_back" 8 android:text="返回数据" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" /> 11 12 </LinearLayout>
3.
MianActivty.java
1 package com.example.app4; 2 3 import android.content.Intent; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.TextView; 9 10 public class MainActivity extends AppCompatActivity { 11 private Button button; 12 private TextView textView; 13 private Intent intent; 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 button = (Button)findViewById(R.id.btn_start); 20 textView = (TextView)findViewById(R.id.tv_show); 21 22 button.setOnClickListener(new View.OnClickListener() { 23 @Override 24 public void onClick(View v) { 25 intent = new Intent(MainActivity.this,SActivity.class); 26 startActivityForResult(intent,0); 27 } 28 }); 29 } 30 31 @Override 32 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 33 super.onActivityResult(requestCode, resultCode, data); 34 if (requestCode==0&&resultCode==0){ 35 String info = data.getStringExtra("info"); 36 textView.setText(info); 37 } 38 } 39 }
SActivty.java
package com.example.app4; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; /** * Created by Administrator on 2018/5/23. */ public class SActivity extends Activity { private Button button; private String content ="你好,我是从SActivity传递回来的数据"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.slayout); button=(Button)findViewById(R.id.btn_back); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.putExtra("info",content); setResult(0,data); finish(); } }); } }