Intent传送对象

转载请注明出处Intent传送对象_Mr_Leixiansheng的博客-CSDN博客

作用: 将对象传递到其他活动

步骤:(与一般数据传送相似)

区别:

1、对象类需要实现Serializable接口

2、接收时 Person person = (Person) getIntent().getSerializableExtra("person_data");

代码如下:

1、类实现接口

package com.example.administrator.intentobj;

import java.io.Serializable;

/**
 * Created by Administrator on 2016/12/16.
 */

public class Person implements Serializable {
    String name;
    String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2、定义跳转界面

package com.example.administrator.intentobj;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/12/16.
 */

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);

        TextView textView = (TextView) findViewById(R.id.textView);

        //获取传递的对象
        Person person = (Person) getIntent().getSerializableExtra("person_data");
        String name = person.name;
        String age = person.age;
        textView.setText("getName is " + name + "\n" + "getAge is " + age);

    }
}
<?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="TextView"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />
</LinearLayout>

3、主程序实现跳转

package com.example.administrator.intentobj;

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

public class MainActivity extends AppCompatActivity {

    private Button button;
    private EditText nameText;
    private EditText ageText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameText = (EditText) findViewById(R.id.name);
        ageText = (EditText) findViewById(R.id.age);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //得到输入内容
                String name = nameText.getText().toString();
                String age = ageText.getText().toString();
                //创建对象
                Person person = new Person();
                person.setAge(age);
                person.setName(name);
                //传递对象
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("person_data", person);
                startActivity(intent);
            }
        });
    }
}
<?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">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TableRow>
            <EditText
                android:id="@+id/name"
                android:hint="请输入姓名"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/age"
                android:hint="请输入年龄"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
        </TableRow>

    </TableLayout>

    <Button
        android:text="传送信息"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button" />
</LinearLayout>

4、注册活动

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.intentobj">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"/>
    </application>

</manifest>

Kotlin写法: 

package com.leixiansheng.kotlintest

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(){


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button.setOnClickListener {
            val name = name.text?.toString() ?: ""
            val age = age.text.toString().toInt()
            val person = Person(name, age)

            val intent = Intent(this, SecondActivity::class.java)
            intent.putExtra("person_data", person)
            startActivity(intent)
        }
    }
}
package com.leixiansheng.kotlintest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.second_layout.*

/**
 * Created by Leixiansheng on 2018/11/15.
 */
class SecondActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.second_layout)

        val person = intent.getSerializableExtra("person_data") as Person

        textView.text = "name:${person.name},age:${person.age}"
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值