Android之旅第三站——活动(Activity)的带返回值的跳转…

来源:互联网 发布:淘宝图片盾在哪里设置 编辑:程序博客网 时间:2024/06/15 02:07

有时候在一个Activity活动结束的时候有些数据是需要回传给上一个Activity的,所以这就用到了带返回值的跳转。

好比网上论坛的注册,在填写完你的注册信息后,返回登录时很多时候直接填写好你的登录信息而不用再次输入,这个用到的就是

带返回值的跳转。

带返回值的跳转有几步骤:

1.从A这个Activity—》跳转到B

Intent intent = new Intent();

intent.setAction(“com.example.d”);

startActivityForResult(intent, REQUESTCODE); // REQUESTCODE是定义好用来匹对传送的信息。

2.在B这个Activity中, 返回一个数据 setReslut(int,intent) 。 这个intent不能有意图

Intent data = new Intent();

data.putExtra(“name”, name);

setResult(RESULT_OK, data);

finish(); //用来结束当前Activity。

3.A 要接受数据 onactivityresult()

if (requestCode == REQUESTCODE & resultCode == RESULT_OK) {//RESULT_OK是定义好用来匹对返回的信息。

String name = data.getStringExtra(“name”);

tv.setText(name);

//只有传来的和返回的匹对成功后才能得到已销毁Activity的返回数据。

接下来咱们就来写一个这样的程序:在第一个Activity通过注册按钮跳转第二个Activity,注册之后返回第一个Activity同时销毁第

二个Activity,将信息填写到第一个Activity。

先来搞定界面:

这里写图片描述

界面xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.tz02.MainActivity"     >    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:text="账号: " />    <EditText         android:id="@+id/et"        android:layout_toRightOf="@id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入账号"        />    <TextView        android:id="@+id/tv1"        android:layout_below="@id/tv"         android:layout_marginTop="10dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="20sp"        android:text="密码: " />    <EditText         android:id="@+id/et1"        android:layout_toRightOf="@id/tv1"       android:layout_marginTop="10dp"        android:layout_below="@id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:password="true"        android:hint="请输入密码"        />    <Button        android:id="@+id/bt"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="注册"        android:textColor="#00f"        android:textSize="30dp"        android:layout_marginTop="10dp"        android:layout_below="@id/et1"        android:layout_marginLeft="20dp"      android:layout_centerHorizontal="true"        /></RelativeLayout>

当然,布局选线性相对都可以。

跳转的第二个Activity的注册界面:

这里写图片描述

界面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"     android:padding="10dp">    <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="账号 :"        android:textSize="20sp"        />    <EditText         android:id="@+id/et"        android:layout_width="match_parent"        android:layout_height="wrap_content"        />    </LinearLayout>     <LinearLayout         android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_marginTop="10dp"        >    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="密码 :"        android:textSize="20sp"        />    <EditText         android:id="@+id/et2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:password="true"        />    </LinearLayout>     <Button          android:id="@+id/bt"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="完成注册"         android:layout_marginTop="10dp"         android:layout_gravity="center_horizontal"         /></LinearLayout>

同样,使用第二个Activity,必须要在androidmanifest中注册该Activity才可以使用,不然会报错。。

之后写逻辑,在第一个Activity中初始化控件及按钮的监听事件(跳转)。

咱们看看返回的结果:

这里写图片描述

重点来了!

package com.example.tz02;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {    private String name;    private String password;    private Button bt;    private EditText et,et1;    private static final int RequestCode=1;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initViews();        registerListeners();    }    private void registerListeners() {        // TODO Auto-generated method stub        bt.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Intent intent=new Intent();                intent.setAction("com.example.tz2");                startActivityForResult(intent, RequestCode);            }        });    }    private void initViews() {        // TODO Auto-generated method stub        bt=(Button) findViewById(R.id.bt);        et=(EditText) findViewById(R.id.et);        et1=(EditText) findViewById(R.id.et1);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        // TODO Auto-generated method stub        super.onActivityResult(requestCode, resultCode, data);        if(requestCode==RequestCode&&resultCode==RESULT_OK)        {            name=data.getStringExtra("name");            password=data.getStringExtra("password");            et.setText(name);            et1.setText(password);        }    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

看起来和之前说的字符串携带数据格式差不多,就是在startActivity变成了startActivityForResult,多了一个onActivityResult方

法,当两个标识都匹对上之后可以获取被销毁Activity的数据,也就是上面提到的第三步。这样在当前Activity填写第二个Activity

注册的信息就很容易了,通过getxxxExtra获得Intent对象中的信息,然后settext控件便可达到效果。

接着看看跳转到的第二个页面:

package com.example.tz02;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class OtherActivity extends Activity {    private EditText et,et1;    private Button bt;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.act_main);        initViews();        registerListeners();    }    private void registerListeners() {        // TODO Auto-generated method stub        bt.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                String name=et.getText().toString().trim();                String password=et.getText().toString().trim();                Intent intent=new Intent();                intent.putExtra("name", name);                intent.putExtra("password", password);                setResult(RESULT_OK, intent);                finish();            }        });    }    private void initViews() {        // TODO Auto-generated method stub        et=(EditText) findViewById(R.id.et);        et1=(EditText) findViewById(R.id.et1);        bt=(Button) findViewById(R.id.bt);    }}

在填写完信息之后,通过控件的gettext获得内容,因为gettext是EditText对象,所以要toString转换成String。之后将得到的内

容通过Intent对象putExtra封装起来, setResult方法来将结果内容Intent对象存入,在finish结束当前Activity时返回,与第一个

Activity中onActivityResult相对应,共同获取被销毁注册Activity中的Intent对象信息。

0 0
原创粉丝点击