//----------Activity1中的布局---------------------------------
<LinearLayout 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:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/tv_text1" android:text="我是Activity1" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/et_edittext1"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳转到Activity2" android:id="@+id/bt_button1"/></LinearLayout>//------------------Activity2的布局文件-------------------------------
<LinearLayout 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:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/tv_text2" android:text="我是Activity2" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/et_edittext2"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="跳转到Activity1" android:id="@+id/bt_button2"/></LinearLayout>//---------------Activity1中-------------------------------
package com.example.test;
import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener { private Button bt_button1; private TextView tv_text1; private EditText et_edittext1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_text1 = (TextView) findViewById(R.id.tv_text1); et_edittext1 = (EditText) findViewById(R.id.et_edittext1); bt_button1 = (Button) findViewById(R.id.bt_button1); bt_button1.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_button1: Intent intent=new Intent(this,TwoActivity.class); String text=et_edittext1.getText().toString().trim(); intent.putExtra("kk", text); startActivityForResult(intent, 0); break; default: break; } } //此方法用户接受返回的数据 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case 1: Bundle b=data.getExtras(); String str=b.getString("ww"); tv_text1.setText(str); break; default: break; } } }//----------------Activity2中----------------------------
package com.example.test;
import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class TwoActivity extends Activity implements OnClickListener { private TextView tv_text2; private EditText et_edittext2; private Button bt_button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); //找到控件 tv_text2 = (TextView) findViewById(R.id.tv_text2); et_edittext2 = (EditText) findViewById(R.id.et_edittext2); bt_button2 = (Button) findViewById(R.id.bt_button2); //获得Activity1传递来的值 Intent intent=getIntent(); String str=intent.getStringExtra("kk"); //显示Activity1传递来的值 tv_text2.setText(str); bt_button2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_button2: String text=et_edittext2.getText().toString().trim(); Intent intent=new Intent(this,MainActivity.class); intent.putExtra("ww", text); //用setResult返回数据 setResult(1, intent); finish(); break; default: break; } } }