Android隐性传值初识

1、传送端代码
 public void go(View view) {
        Intent intent = new Intent();
       Bundle bundle =  new Bundle();
        User user = new User();
        user.setAge(99);
        user.setName("Li");
      //  intent.putExtra("name","zhang");
      //  intent.putExtra("age",100);

        intent.setAction("user1");
        bundle.putSerializable("user",user);
        intent.putExtras(bundle);
        startActivity(intent);
    }

2、接收端接受全送过来的值

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //Intent intent = this.getIntent();
      //  String name = intent.getStringExtra("name");
    // int age =    intent.getIntExtra("age", 120);
       // Log.i("dong", "onCreate....main2..."+name+".."+age);
        Intent intent =  this.getIntent();
        User user = (User)intent.getSerializableExtra("user");
        Log.i("dong", "onCreate....main2..."+user.getName()+".."+user.getAge());

    }

3、注意选择serializable接口可以传送一对象
4、隐性传值必须在xmlz这样写

<activity android:name=".Main2Activity">
            <intent-filter>
                <action android:name="user1"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
        </activity>

上面是今天自己下课摸索的,晚上回去再写点其他难一点的。

 

你可能感兴趣的:(android)