Intent传送对象
2016-12-16 15:16
88 查看
转载请注明出处:http://blog.csdn.net/mr_leixiansheng/article/details/53693616
作用: 将对象传递到其他活动
步骤:(与一般数据传送相似)
区别:
1、对象类需要实现Serializable接口
2、接收时 Person person = (Person) getIntent().getSerializableExtra("person_data");
代码如下:
1、类实现接口
作用: 将对象传递到其他活动
步骤:(与一般数据传送相似)
区别:
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>
相关文章推荐
- Intent传递对象
- Intent 传递对象 Serializable/Parcelable
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- 使用Intent在活动之间传递对象
- 使用Bundle对象在Activity之间传送数据
- Android---Intent传递对象、对象数组
- android用intent传递对象
- Android中intent传递对象和Bundle的用法
- intent几种传值数组、对象、集合(Array,Object,List)
- Intent.putExtra()传递Object对象或者ArrayList<Object>
- Intent序列化传递对象
- WCF中使用MSMQ无法传送自定义对象
- Android中Intent传递对象的两种方法(Serializable,Parcelable)
- Contacts中Intent对象的使用
- Android中Intent传递对象的3种方式详解
- Android中Intent传递对象
- Android中Intent传递 对象 的两种方法(Serializable,Parcelable) .
- Android高手进阶教程(十七)之---Android中Intent传递对象的两种方法(Serializable,Parcelable)!
- Intent传递对象信息方式Parcelable和Serializable(序列化)
- 教你一招:在多个Activity之间传递对象,不用Bundle,Intent.putExtra