新建项目:
项目结构:
运行效果:
回到MainActivity类
代码部分:
main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="请输入:" 11 /> 12 <EditText 13 android:id="@+id/et_input" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 android:hint="内容" 17 ></EditText> 18 <Button 19 android:id="@+id/btn_send" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="跳转" 23 ></Button> 24 </LinearLayout>
MainActivity.java
1 package com.b510; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.EditText; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 /** 定义一个输入框 */ 14 private EditText et_input; 15 /** 定义一个按钮 */ 16 private Button btn_send; 17 /** 定义一个requestCode为0 */ 18 private final static int REQUEST_CODE = 0; 19 20 /** Called when the activity is first created. */ 21 @Override 22 public void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.main); 25 26 et_input = (EditText) findViewById(R.id.et_input); 27 btn_send = (Button) findViewById(R.id.btn_send); 28 btn_send.setOnClickListener(new OnClickListener() { 29 public void onClick(View v) { 30 Intent intent = new Intent(MainActivity.this, 31 ReceiveAcitivity.class); 32 // 把et_input输入框中的信息设置在参数msg中 33 intent.putExtra("msg", et_input.getText().toString()); 34 // 执行跳转 35 startActivityForResult(intent, REQUEST_CODE); 36 } 37 }); 38 } 39 40 /** 41 * 当从其他页面返回来的时候,会调用此方法 42 */ 43 @Override 44 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 45 super.onActivityResult(requestCode, resultCode, data); 46 if (requestCode == REQUEST_CODE) { 47 if (resultCode == ReceiveAcitivity.RUSULT_CODE_1) { 48 Toast.makeText(MainActivity.this, "这是从ReceiveActivity类回来的响应", 49 Toast.LENGTH_LONG).show(); 50 } 51 } 52 } 53 }
receive.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:layout_width="fill_parent" 9 android:layout_height="wrap_content" 10 android:text="你输入的是:" 11 /> 12 <TextView 13 android:id="@+id/tv_receive" 14 android:layout_width="fill_parent" 15 android:layout_height="wrap_content" 16 /> 17 18 <Button 19 android:id="@+id/btn_return" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:text="返回" 23 ></Button> 24 </LinearLayout>
ReceiveActivity.java
1 package com.b510; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.view.View; 7 import android.view.View.OnClickListener; 8 import android.widget.Button; 9 import android.widget.TextView; 10 11 /** 12 * @author Hongten 13 * 14 */ 15 public class ReceiveAcitivity extends Activity { 16 17 /** 定义一个resultCode=1 */ 18 public final static int RUSULT_CODE_1 = 1; 19 /** 定义一个接受消息的TextView */ 20 private TextView tv_receive; 21 /** 定义一个返回按钮 */ 22 private Button btn_return; 23 /** 定义一个Intent对象 */ 24 Intent intent; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.receive); 30 31 tv_receive = (TextView) findViewById(R.id.tv_receive); 32 btn_return = (Button) findViewById(R.id.btn_return); 33 intent = getIntent(); 34 // 接收从MainActivity类传递过来的信息msg 35 String msg = intent.getStringExtra("msg"); 36 // 设置tv_receive的值为msg 37 tv_receive.setText(msg); 38 // 当点击返回按钮的时候,返回到MainActivity类 39 btn_return.setOnClickListener(new OnClickListener() { 40 public void onClick(View v) { 41 setResult(RUSULT_CODE_1); 42 finish();// finish本Activity 43 } 44 }); 45 } 46 }
AndroidManifest.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.b510" 4 android:versionCode="1" 5 android:versionName="1.0"> 6 <uses-sdk android:minSdkVersion="9" /> 7 8 <application android:icon="@drawable/icon" android:label="@string/app_name"> 9 <activity android:name=".MainActivity" 10 android:label="@string/app_name"> 11 <intent-filter> 12 <action android:name="android.intent.action.MAIN" /> 13 <category android:name="android.intent.category.LAUNCHER" /> 14 </intent-filter> 15 </activity> 16 <activity android:name=".ReceiveAcitivity" 17 android:label="@string/app_name"> 18 </activity> 19 </application> 20 </manifest>