package com.example.objecttrandemo;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = -7060210544600464481L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="android object trance" /> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Serializable" /> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Parcelable" /> </LinearLayout>
package com.example.objecttrandemo;
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;
public class ObjectTranDemo extends Activity implements OnClickListener{
private Button sButton,pButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
//initial the views
public void setupViews(){
sButton = (Button)findViewById(R.id.button1);
pButton = (Button)findViewById(R.id.button2);
sButton.setOnClickListener(this);
pButton.setOnClickListener(this);
}
//Serializeable object trance
public void SerializeMethod(){
Person mPerson = new Person();
mPerson.setName("durant");
mPerson.setAge(25);
Intent mIntent = new Intent(this,ObjectTranDemo1.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("mPerson",mPerson);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
//Pacelable object trance
public void PacelableMethod(){
Book mBook = new Book();
mBook.setBookName("a man from mars");
mBook.setAuthor("james");
mBook.setPublishTime(2014);
Intent mIntent = new Intent(this,ObjectTranDemo2.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable("mBook", mBook);
mIntent.putExtras(mBundle);
startActivity(mIntent);
}
@Override
public void onClick(View v) {
if(v == sButton){
SerializeMethod();
}else{
PacelableMethod();
}
}
}
package com.example.objecttrandemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo1 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
Person mPerson = (Person)getIntent().getSerializableExtra("mPerson");
mTextView.setText("You name is: " + mPerson.getName() + "\n"+
"You age is: " + mPerson.getAge());
setContentView(mTextView);
}
}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.objecttrandemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.objecttrandemo.ObjectTranDemo" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ObjectTranDemo1"></activity> <activity android:name=".ObjectTranDemo2"></activity> </application> </manifest>
package com.example.objecttrandemo;
import android.os.Parcel;
import android.os.Parcelable;
public class Book implements Parcelable{
private String bookName;
private String author;
private int publishTime;
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPublishTime() {
return publishTime;
}
public void setPublishTime(int publishTime) {
this.publishTime = publishTime;
}
//Internal Description Interface,You do not need to manage
@Override
public int describeContents() {
return 0;
}
//give some attention to the oder betwwen writeToParcel and createFromParcel
@Override
public void writeToParcel(Parcel parcel, int flags){
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book[] newArray(int size) {
return new Book[size];
}
@Override
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
};
}
package com.example.objecttrandemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ObjectTranDemo2 extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView mTextView = new TextView(this);
Book mBook = (Book)getIntent().getParcelableExtra("mBook");
mTextView.setText("Book name is: " + mBook.getBookName()+"\n"+
"Author is: " + mBook.getAuthor() + "\n" +
"PublishTime is: " + mBook.getPublishTime());
setContentView(mTextView);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有