demo下载地址http://download.csdn.net/detail/dl10210950/9590558
这篇博客实现的逻辑是带返回值的跳转,
从ActivityA跳转到ActivityB,然后结束ActivityB后带返回值到ActivityA。下图就是运行的结果,模拟登陆注册
ActivityA用到的方法是startActivityForResult(intent, requestCode);
参数1:intent意图, 第二个参数请求码,就是告诉是ActivityA发起的请求,主要是为了区分是哪个activity请求别的activity带返回值过来,就像两个共产党接头,ActivityA带一个暗号,ActivityB带一个暗号,两个人的暗号都对上才算是接头成功。
ActivityB里面用到的方法是setResult(resultCode, data);
参数1resultCode:是一个返回码,就是ActivityB所带的暗号
参数2data:就是数据源
接下来就来代码中实现
首先是ActivityA中的布局文件
<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="wrap_content" android:textSize="25sp" android:gravity="center_horizontal" android:text="登陆界面"/> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="30sp" android:gravity="center_vertical" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:text="账号" /> <EditText android:id="@+id/activitya_name" android:layout_width="0dp" android:gravity="center_vertical" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:textSize="30sp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:gravity="center_vertical" android:text="密码" /> <EditText android:id="@+id/activitya_password" android:layout_width="0dp" android:gravity="center_vertical" android:layout_height="match_parent" android:layout_weight="1" /> </LinearLayout> <Button android:onClick="register" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="去注册" /></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
两个输入框,一个button按钮,点击跳转到ActivityB去注册
ActivityA中的具体逻辑代码
package com.example.intentdemo2forresult;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class ActivityA extends Activity { private EditText edt_name; private EditText edt_pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); initView(); } /** * 用来初始化控件 */ private void initView() { edt_name = (EditText) findViewById(R.id.activitya_name); edt_pwd = (EditText) findViewById(R.id.activitya_password); } /** * button点击事件 */ public void register(View view) { Intent intent = new Intent(ActivityA.this, ActivityB.class); startActivityForResult(intent, 222); } /** * 所有的Activity对象的返回值都是由这个方法来接收 requestCode: * 表示的是启动一个Activity时传过去的requestCode值 * resultCode:表示的是启动后的Activity回传值时的resultCode值 * data:表示的是启动后的Activity回传过来的Intent对象 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 222 && resultCode == 111) { String name = data.getStringExtra("name"); String pwd = data.getStringExtra("pwd"); edt_name.setText(name); edt_pwd.setText(pwd); } }}ActivityB中的布局文件是
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
ActivityB中具体的逻辑代码
package com.example.intentdemo2forresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class ActivityB extends Activity {
private EditText edt_name;// 注册时输入的账号
private EditText edt_pwd;// 注册时输入的密码
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); initView();}/** * 用来初始化控件 */private void initView() { edt_name = (EditText) findViewById(R.id.activityb_name); edt_pwd = (EditText) findViewById(R.id.activityb_password);}/** * button的点击事件 */public void register(View view) { //拿到账号输入框输入的值trim()是去空格的方法 String name = edt_name.getText().toString().trim(); //拿到密码输入框输入的值trim()是去空格的方法 String pwd = edt_pwd.getText().toString().trim(); //拿到一个intent把需要返回的值放进去 Intent intent = new Intent(); intent.putExtra("name", name); intent.putExtra("pwd", pwd); /* * 调用setResult方法表示我将Intent对象返回给之前的那个Activity,这样就可以在onActivityResult方法中得到Intent对象, * 参数1:resultCode返回码,跳转之前的activity根据是这个resultCode,区分是哪一个activity返回的 * 参数2:数据源 */ setResult(111, intent); finish();//结束当前activity}
}
看不懂的朋友可以把代码复制到项目里,然后注册两个activity就可以运行看效果了
欢迎指正,虚心接受