Android 实战开发 页面跳转数据传递

本文通过实例演示了如何在Android应用中实现页面间的跳转及数据传递,包括使用Intent和Bundle来封装数据,并展示了如何在目标页面中获取这些数据。

Android 实战开发 页面间跳转已经实现过了,页面之间数据传递是怎么样的,做一个demo进行实现

一、页面结构


二 代码实现

  1.第二页 按钮 回调

      

@Override

public void onClick(View v)

{

    Intent intent = new  Intent();

    intent.setClass(this,SecondActivity.class);

    Bundle bundle = new Bundle();

    bundle.putString("name","凌晨");

    bundle.putInt("time",24);

    intent.putExtras(bundle);

    startActivityForResult(intent, 1001);

}

2.第三页 接收上一页的数据传递

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_next_next);

    closeBtn = (Button)findViewById(R.id.closeButton);

    closeBtn.setOnClickListener(this);

    TextView textView = findViewById(R.id.secondview);

 
 

    Intent intent = getIntent();

    Bundle bundle = intent.getExtras();

    String name = bundle.getString("name");

    int age = bundle.getInt("time");

    textView.setText(name +age+"点");

}

3.第三页 按钮回调并封装数据

@Override

public void onClick(View v)

{

    Intent intent = new  Intent();

    Bundle bundle = new Bundle();

    bundle.putString("name","我返回了!");

    intent.putExtras(bundle);

    setResult(Activity.RESULT_OK,intent);

    finish();

}

4. 第二页接收第三页返回来的数据

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 if (data != null) {

     TextView textView = findViewById(R.id.tiptview);

     Intent intent = getIntent();

     String name = data.getStringExtra("name");

     textView.setText(name);

  }

}

代码显示完毕。

效果


总结:在学习中

onActivityResult 一直不被调用,查其原因是因为 在父类的父类中 声明为保护类型 所以直接继承,既可以解决!


   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值