Android 开发入门篇 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

Activity传递参数

Activity传递参数的方法有两种:

1.使用Bundle,然后使用Intent
2.直接使用Intent传递参数

虽然都会使用Intent但是直接使用Intent传递的都是单个参数,使用Bundle传递一组数据。

传递单个参数:

    Intent intent = new Intent(MainActivity.this , SecondActivity.class);
    intent.putExtra("key", "传递的参数");
     startActivity(intent);

获取参数:(在onCreate下即可)

    Intent get = getIntent();
    get.getStringExtra("key");

使用Bundle传递多个参数:

    Intent intent = new Intent(MainActivity.this , SecondActivity.class);
    Bundle bundle = new Bundle();
    bundle.putString("name", "张三");
    bundle.putInt("age", 2333);
    intent.putExtras(bundle);
    startActivity(intent);

获取参数:

    Intent get = getIntent();
    Bundle bun = get.getExtras();
    String name = bun.getString("name");
    int age = bun.getInt("age");
自己写个登陆页面,点击登陆后将参数传递给第二个页面。 参考代码(4-4):https://github.com/hubwiz/android-rudiments