Android—页面间传递数据

一、布局文件

activity_main.xml

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

   <EditText
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:hint="请输入你的名字"
       android:id="@+id/et_main_name"
       />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="确定"
        android:onClick="Qd"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="5"
        android:id="@+id/tv_main_text"
        android:hint="收到one的消息为"
        />


</LinearLayout>

activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.myapplication.TwoActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="1"
        android:id="@+id/tv_one_name"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:lines="4"
        android:hint="请输入你的基本信息"
        android:gravity="top"
        android:id="@+id/et_one_text"
        />
     <Button
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="提交"
         android:onClick="commit"
         />


</LinearLayout>

二、Java代码

相同代码:

实体类:Person.java

package com.sun.entity;

import android.os.Parcel;
import android.os.Parcelable;

import java.io.Serializable;

/**
 * Created by Administrator on 2017/6/16 0016.
 */
           //Java 序列化实现Serializable  Android序列化实现Parcelable

public class Person implements Parcelable{
    private int pid;
    private String pname;
    private int page;

    public Person() {

    }

    public Person(int pid, String pname, int page) {
        this.pid = pid;
        this.pname = pname;
        this.page = page;
    }

    protected Person(Parcel in) {
        pid = in.readInt();
        pname = in.readString();
        page = in.readInt();
    }

    public static final Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    public int getPid() {
        return pid;
    }

    public String getPname() {
        return pname;
    }

    public int getPage() {
        return page;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public void setPage(int page) {
        this.page = page;
    }

    @Override
    public String toString() {
        return "Person{" +
                "pid=" + pid +
                ", pname='" + pname + '\'' +
                ", page=" + page +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(pid);
        dest.writeString(pname);
        dest.writeInt(page);
    }
}


MianActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sun.entity.Person;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private EditText et_main_name;
    private TextView tv_main_text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件
        et_main_name = (EditText) findViewById(R.id.et_main_name);
        tv_main_text = (TextView) findViewById(R.id.tv_main_text);
    }

    public void Qd(View view){
          //跳转页面
       
//新建一个显式意图,第一个参数为当前Activity类对象,第二个参数为你要打开的Activity类
Intent intent=new Intent(this,TwoActivity.class); //--此处填写传递数据的代码--} @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); String content=data.getStringExtra("content"); tv_main_text.setText(content); }}



①传递基本数据类型

        intent.putExtra("uname",uname);
②bundle集装箱传递
        Bundle bundle=new Bundle();
        bundle.putString("uname",uname);
        bundle.putInt("uage",19);
        intent.putExtra("bundle",bundle);
③传递数据第三种(对象传递)(实体类实现java序列化接口Serializable)可传递
        Person person=new Person(1,"天空一号",18);
        intent.putExtra("person",person);
④传递数据第四种(对象传递)(实体类实现android序列化接口Parcelable)
 Person person=new Person(1,"周宇",20);
        intent.putExtra("person",person);

⑤传递数据=第五种(对象集合传递)(实体类实现android序列化接口Parcelable)
        
        ArrayList<Person> persons=new ArrayList<>();
        Person person1=new Person(1,"嫦娥1号",18);
        Person person2=new Person(2,"嫦娥2号",18);
        Person person3=new Person(3,"嫦娥3号",18);
        persons.add(person1);
        persons.add(person2);
        persons.add(person3);
        intent.putParcelableArrayListExtra("persons",persons);

Person person=new Person(1,"周宇",20); intent.putExtra("person",person);

传递数据两种方式
1、普通传递,不用再传数据回来时,用次方法
startActivity(intent);
2、高级传递,需要再传数据回来时
startActivityForResult(intent,0x201); //参数是十六进制码

重写方法接受来自One的结果
   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String content=data.getStringExtra("content");
        tv_main_text.setText(content);
    }

TwoActivity.java
package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sun.entity.Person;

import java.util.List;

public class TwoActivity extends AppCompatActivity {

    private EditText et_one_text;
    private TextView tv_one_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        et_one_text = (EditText) findViewById(R.id.et_one_text);
        tv_one_name = (TextView) findViewById(R.id.tv_one_name);

         //--此处接收来自Main界面的数据--
      
    }
    //传值去第一个界面
    public void commit(View view){
        String  content=et_one_text.getText().toString();
        //传递数据第一种(普通传递)
        Intent intent=new Intent(this,MainActivity.class);
        intent.putExtra("content",content);
        //第一种传值
       // startActivity(intent);
        //第二种传值(设置结果)
        setResult(0x100,intent);
        //自杀(从堆栈中移除)→干掉自己,干掉当前的Activity
        finish();
    }




}

接收值
①第一种  接收普通传递
  String uname=getIntent().getStringExtra("uname");
        tv_one_name.setText(uname);
②第二种  接收bundle
 Bundle bundle=getIntent().getBundleExtra("bundle");
        String uname=bundle.getString("uname");
        int uage=bundle.getInt("uage");
        tv_one_name.setText("uname:"+uname+"uage:"+uage);
③第三种  接收对象
 Person person= (Person) getIntent().getSerializableExtra("person");
        tv_one_name.setText(person.toString());
④第四种  接收对象
 Person person=getIntent().getParcelableExtra("person");
        tv_one_name.setText(person.toString());
⑤第五种  接收对象集合
 List<Person> persons=getIntent().getParcelableArrayListExtra("persons");
        for (Person person : persons) {
            tv_one_name.setText(persons.toString());
        }









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值