Android--启动另一个Activity并返回结果

在注册一个系统会员时经常需要多个步骤,经常使用“上一步”、“下一步”来完成,以下示例是用户在点击“上一步”时保存信息,使用startActivityForResult().

MainActivity代码如下所示:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private Button b1;
    private EditText username,password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b1 = (Button) findViewById(R.id.btn01);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                username = (EditText) findViewById(R.id.username);
                password = (EditText) findViewById(R.id.password);
                //获得用户名称
                String str_username = username.getText().toString();
                //获得密码
                String str_password = password.getText().toString();
                // 实例化Bundle
                Bundle b = new Bundle();
                //为Bundle添加用户名称和密码
                b.putString("username",str_username);
                b.putString("password",str_password);
                //实例化Intent
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                //将Bundle添加到intent
                intent.putExtras(b);
                startActivityForResult(intent,0);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //从Intent中获得Bundle
        Bundle b = data.getExtras();
        //从Bundle中获得用户名称、密码
        String str_username = b.getString("username");
        String str_password = b.getString("password");
        //将用户名和密码赋值给EditText
        username.setText(str_username);
        password.setText(str_password);
    }
}

NextActivity 代码如下:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NextActivity extends Activity {
    private Button b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next);
        b2 = (Button) findViewById(R.id.btn02);
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = getIntent();
                NextActivity.this.setResult(0, intent);
                NextActivity.this.finish();
            }
        });
    }
}

布局文件main.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvm1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvm01"></TextView>

    <EditText
        android:id="@+id/username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""></EditText>

    <TextView
        android:id="@+id/tvm02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/tvm02"></TextView>

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:text=""></EditText>

    <Button
        android:id="@+id/btn01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn01"></Button>


</LinearLayout>

布局文件next.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="@string/email"
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" ></TextView>

    <EditText
        android:text=""
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" ></EditText>

    <TextView
        android:text="@string/mobile"
        android:id="@+id/mobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" ></TextView>

    <EditText
        android:text=""
        android:id="@+id/EditText02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" ></EditText>

    <Button
        android:text="@string/btn02"
        android:id="@+id/btn02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

注:在AndroidManifest.xml中注册

<activity android:name=".NextActivity"></activity>

调试结果:
这里写图片描述 这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值