标签:
package com.example.helloworld2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class A_Activity extends Activity { private TextView text; private final static int REQUESTCODE = 1; private final static int RESULTCODE = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView) findViewById(R.id.text); } public void go(View v){ Intent intent = new Intent(A_Activity.this,B_Activity.class); startActivityForResult(intent, REQUESTCODE); } @Override protected void onActivityResult(int requsetCode, int resultCode, Intent intent) { // TODO Auto-generated method stub super.onActivityResult(requsetCode, resultCode, intent); if(resultCode==RESULTCODE){ Bundle bundle = intent.getExtras(); String result = bundle.getString("result"); text.setText("返回的值:"+result+""); } } }
在B中:
public class B_Activity extends Activity { private EditText text2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); text2 = (EditText) findViewById(R.id.text2); } /** * 返回 * @param v */ public void go(View v){ Intent intent = new Intent(); intent.putExtra("result", text2.getText().toString().trim()); setResult(2, intent); //将结果出给A finish();//此处一定要调用finish()方法 } }
布局文件:
<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" tools:context="com.example.helloworld.MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="go" android:text="跳转"/> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </LinearLayout>
<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" tools:context="com.example.helloworld.MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="go" android:text="返回"/> <EditText android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="100" /> </LinearLayout>
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".A_Activity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.helloworld2.B_Activity" > </activity> </application>
Activity 的onActivityResult使用之旅
标签:
原文地址:http://www.cnblogs.com/xiaoxiao-study/p/c38d95f987e0d6d0187fafbfdc64c582.html