package com.example.androidtest_parcel; import android.os.Bundle; import android.os.Parcel; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener{ private Button mWI,mWF,MWD; private Button mRI,mRF,mRD; private TextView mTextView1; private EditText mEdit; private Parcel mParcel; private int position; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initListener(); //获取Parcel对象 mParcel = Parcel.obtain(); } public void initView(){ mWI= (Button)findViewById(R.id.writeInt); mWF= (Button)findViewById(R.id.writeFloat); MWD= (Button)findViewById(R.id.writeDouble); mRI= (Button)findViewById(R.id.readInt); mRF= (Button)findViewById(R.id.readFloat); mRD= (Button)findViewById(R.id.readDouble); mEdit = (EditText)findViewById(R.id.writePosition); mTextView1 = (TextView)findViewById(R.id.textView1); } public void initListener(){ mWI.setOnClickListener(this); mWF.setOnClickListener(this); MWD.setOnClickListener(this); mRI.setOnClickListener(this); mRF.setOnClickListener(this); mRD.setOnClickListener(this); mTextView1 = (TextView)findViewById(R.id.textView1); } public void showMyParcelDouble(){ int allSize = mParcel.dataCapacity();//当前分配的空间 int realSize = mParcel.dataSize();//当前实际存储空间 mParcel.setDataPosition(Integer.parseInt(mEdit.getText().toString())); int realPosition = mParcel.dataPosition();//当前偏移量 mTextView1.setText("当前分配空间:"+allSize+'\n'+ "当前实际存储空间:"+realSize+'\n'+ "当前偏移量:"+realPosition+'\n'+ '\n'+ "当前读取到的数为:"+mParcel.readDouble()); } public void showMyParcelFloat(){ int allSize = mParcel.dataCapacity();//当前分配的空间 int realSize = mParcel.dataSize();//当前实际存储空间 mParcel.setDataPosition(Integer.parseInt(mEdit.getText().toString())); int realPosition = mParcel.dataPosition();//当前偏移量 mTextView1.setText("当前分配空间:"+allSize+'\n'+ "当前实际存储空间:"+realSize+'\n'+ "当前偏移量:"+realPosition+'\n'+ '\n'+ "当前读取到的数为:"+mParcel.readFloat()); } public void showMyParcelInt(){ int allSize = mParcel.dataCapacity();//当前分配的空间t int realSize = mParcel.dataSize();//当前实际存储空间 mParcel.setDataPosition(Integer.parseInt(mEdit.getText().toString())); int realPosition = mParcel.dataPosition();//当前偏移量 mTextView1.setText("当前分配空间:"+allSize+'\n'+ "当前实际存储空间:"+realSize+'\n'+ "当前偏移量:"+realPosition+'\n'+ '\n'+ "当前读取到的数为:"+mParcel.readInt()); } public void showMyParcel(){ int allSize = mParcel.dataCapacity();//当前分配的空间 int realSize = mParcel.dataSize();//当前实际存储空间 int realPosition = mParcel.dataPosition();//当前偏移量 mTextView1.setText("当前分配空间:"+allSize+'\n'+ "当前实际存储空间:"+realSize+'\n'+ "当前偏移量:"+realPosition+'\n'+ '\n'+ "当前没有读取"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.writeInt: mParcel.writeInt(10); showMyParcel(); break; case R.id.writeFloat: mParcel.writeFloat(20); showMyParcel(); break; case R.id.writeDouble: mParcel.writeDouble(30); showMyParcel(); break; case R.id.readInt: showMyParcelInt(); break; case R.id.readFloat: showMyParcelFloat(); break; case R.id.readDouble: showMyParcelDouble(); break; default: break; } } }
<LinearLayout 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" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/writeInt" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="写入一个int" /> <Button android:id="@+id/readInt" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="读取一个int" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/writeFloat" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="写入一个float" /> <Button android:id="@+id/readFloat" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="读取一个float" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/writeDouble" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="写入一个double" /> <Button android:id="@+id/readDouble" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="读取一个double" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:text="写入偏移量:" android:textSize="20dp"/> <EditText android:id="@+id/writePosition" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:hint="此处输入偏移量" /> </LinearLayout> <TextView android:layout_marginTop="10dp" android:id="@+id/textView1" android:layout_width="fill_parent" android:layout_height="200dp"/> </LinearLayout>
原文地址:http://blog.csdn.net/u010794180/article/details/43791691