通过Intent传递单个或多个值

通过Intent传递单个或多个值,第1张

概述活动与活动之间传递参数以及活动界面的跳转,都用到Intent,这里我们先创建活动,在活动MainActivity中生成一个按钮用于传递参数以及界面的跳转intent主要用了以下方法进行传输:以下是单一参数的传输:Intentintent=newIntent(MainActivity.this,SecondActivity.class);int

活动与活动之间传递参数以及活动界面的跳转,都用到Intent,
这里我们先创建活动,在活动MainActivity中生成一个按钮用于传递参数以及界面的跳转
intent主要用了以下方法进行传输:
以下是单一参数的传输:

Intent intent =new Intent(MainActivity.this,SecondActivity.class);intent.putExtra("key","value");startActivity(intent);@H_419_13@

多个参数的传输通过bundle这一媒介来进行传输:

Bundle bundle =new Bundle();bundle.putString("key","value");bundle.putString("key2","value2");intent.putExtras(bundle);@H_419_13@
@H_502_21@package com.game.intent822;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.linearLayout;public class MainActivity extends AppCompatActivity {private linearLayout layout;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        final  Bundle bundle =new Bundle();        bundle.putString("name","jack");        bundle.putString("name2","job");        button button = new button(this);        layout=(linearLayout)findVIEwByID(R.ID.layout);        layout.addVIEw(button);        button.setText("send");        button.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw vIEw) {                Intent intent = new Intent(MainActivity.this,SecondActivity.class);                intent.putExtras(bundle);                startActivity(intent);            }        });    }}@H_419_13@

然后在SecondActivity中对传输的参数进行接收,

Intent intent =getIntent();intent.getStringExtra("key");@H_419_13@
@H_502_21@package com.game.intent822;import androID.content.Intent;import androID.os.Bundle;import androID.Widget.TextVIEw;import androIDx.annotation.Nullable;import androIDx.appcompat.app.AppCompatActivity;public class SecondActivity extends AppCompatActivity {    @OverrIDe    protected voID onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_second);        TextVIEw textVIEw=(TextVIEw)findVIEwByID(R.ID.tv);        Intent intent = getIntent();       String item =intent.getStringExtra("name");       String item2=intent.getStringExtra("name2");        textVIEw.setText(item+" \n"+item2);    }}@H_419_13@          总结       

以上是内存溢出为你收集整理的通过Intent传递单个或多个值全部内容,希望文章能够帮你解决通过Intent传递单个或多个值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1111721.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存