Android 通过Intent bundle 传递复杂对象
2016-07-24 00:00
246 查看
摘要: 对intent 进行更近一部的了解,进行更复杂的应用。
activity_main.xml
next.xml部分
Activity部分
MainActivity.java 部分
NextActivity.java 部分
Person.java 部分
传递复杂对象(传递自定义对象)
XML部分activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="传递复杂对象" android:id="@+id/button1" /> </RelativeLayout>
next.xml部分
<?xml version="1.0" encoding="utf-8"?> <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" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/msg" android:textSize="30dp" /> </RelativeLayout>
Activity部分
MainActivity.java 部分
package com.example.hx.myapplication; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button)findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(MainActivity.this,NextActivity.class);//创建Intent 意图 Person person=new Person();//实现NextActivity传递一个复杂对象 person.setAge(23); person.setName("jajj"); intent.putExtra("msg",person); startActivity(intent); } }); } }
NextActivity.java 部分
package com.example.hx.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class NextActivity extends AppCompatActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.next); textView=(TextView)findViewById(R.id.msg); Person person=(Person) getIntent().getParcelableExtra("msg"); textView.setText("-->>>"+person.toString()); } }
Person.java 部分
package com.example.hx.myapplication; import android.os.Parcel; import android.os.Parcelable; /** * Created by hx on 2016/7/24. */ public class Person implements Parcelable{ private int age; private String name; @Override public String toString() { return "Person{" + "age=" + age + ", name='" + name + '\'' + '}'; } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public int getAge() { return age; } private Person(Parcel parcel){ age =parcel.readInt(); name=parcel.readString(); } public Person(){} @Override public int describeContents() { return 0; } public static final Parcelable.Creator<Person> CREATOR= new Creator<Person>() { @Override public Person createFromParcel(Parcel parcel) { return new Person(parcel); } @Override public Person[] newArray(int i) { return new Person[i]; } }; @Override public void writeToParcel(Parcel parcel, int i) { //向pacel写入序列化得数据 parcel.writeInt(age); parcel.writeString(name); } }
相关文章推荐
- php开发中的页面跳转方法总结
- JS option location 页面跳转实现代码
- Javascript页面跳转常见实现方式汇总
- js实现页面跳转的五种方法推荐
- js实现页面跳转的五种方法推荐
- jquery mobile页面跳转后样式丢失js失效的解决方法
- 五种 JSP页面跳转方法详解
- Ruby rails 页面跳转(render和redirect_to)
- Android 使用Intent传递数据的实现思路与代码
- javascript 页面跳转方法集合
- jQuery Mobile页面跳转后未加载外部JS原因分析及解决
- Android组件间通信--深入理解Intent与IntentFilter
- PHP 页面跳转到另一个页面的多种方法方法总结
- Android中Intent机制详解及示例总结(总结篇)
- Android使用Intent发送短信的实现方法
- 学习Ruby你需要了解的相关知识(rvm, gem, bundle, rake, rails等)
- Rails bundle命令安装mysql gem包出错的解决方法
- ASP.NET MVC中使用Bundle打包压缩js和css的方法
- 三种asp.net页面跳转的方法
- PHP Header用于页面跳转要注意的几个问题总结