zoukankan      html  css  js  c++  java
  • Parcelable使用(二)

    简单的Activity间数据传递用Intent,复杂的用Parcelable,举个栗子第一个activity写入Parcel的String类型的name和int类型的age,第二个activity取出Parcel并解析数据

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button button = findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    MyPerson mMyPerson = new MyPerson();
                    mMyPerson.setName("tom");
                    mMyPerson.setAge(25);
                    Intent mIntent = new Intent(MainActivity.this, Main2Activity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putParcelable("key", mMyPerson);
                    mIntent.putExtras(mBundle);
                    startActivity(mIntent);
    
    
                }
            });
        }
    }

       

    上面是第一个activity

    public class Main2Activity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            TextView textView = findViewById(R.id.text);
            MyPerson parcelable = getIntent().getParcelableExtra("key");
            textView.setText("name = " + parcelable.getName() + "  age = " + parcelable.getAge());
    
    
        }
    }

    上面试第二个activity

    public class MyPerson implements Parcelable{
        private String name;
        private int age;
        private static final String TAG = "Text";
        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;
        }
        public static final Parcelable.Creator<MyPerson> CREATOR = new Creator<MyPerson>() {
            @Override
            public MyPerson createFromParcel(Parcel source) {
                Log.d(TAG,"createFromParcel");
                MyPerson mMyPerson = new MyPerson();
                mMyPerson.name = source.readString();
                mMyPerson.age = source.readInt();
                return mMyPerson;
            }
            @Override
            public MyPerson[] newArray(int size) {
    // TODO Auto-generated method stub
                return new MyPerson[size];
            }
        };
        @Override
        public int describeContents() {
    // TODO Auto-generated method stub
            Log.d(TAG,"describeContents");
            return 0;
        }
        @Override
        public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
            Log.d(TAG,"writeToParcel");
            dest.writeString(name);
            dest.writeInt(age);
        }
    }

    实现Parcelable接口

  • 相关阅读:
    爬取豆瓣分页照片下载
    css布局:三列布局,中间内容优先加载
    解决在IE下label中IMG图片无法选中radio的几个方法
    CSS架构:最新最佳实践
    JavaScript登陆弹窗,可拖拽
    网站变成灰色的代码
    5个jQuery的备选轻量级移动客户端开发(Mobile development)类库
    jQuery 底部漂浮导航当前位置突出 + 锚点平滑滚动
    Exchange 2007 自定义传输规则
    基于jQuery打造TabPanel
  • 原文地址:https://www.cnblogs.com/Ocean123123/p/10955721.html
Copyright © 2011-2022 走看看