将多个变量值发送到另一个活动

发布于 2025-01-07 15:50:30 字数 689 浏览 2 评论 0原文

这是我用来将字符串变量的值传输到另一个活动的代码。

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);

但是如果我想传输多个变量怎么办?

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);


            Intent userSearch = new Intent(Search.this, Results.class);
            userSearch.putExtra("Search", addressInput);
            startActivity(userSearch);

使用代码两次将像上面一样只会启动两个单独的活动。那么,如何同时传输这些值呢?

我对 Android 开发和 OOP 还很陌生。

So this is the code that I use to transfer the value of a string variable to another activity.

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);

But what if I wish to transfer more than one variable.

            Intent requestLink = new Intent(Search.this, Results.class);
            requestLink.putExtra("Link", sendLink);
            startActivity(requestLink);


            Intent userSearch = new Intent(Search.this, Results.class);
            userSearch.putExtra("Search", addressInput);
            startActivity(userSearch);

Using the code twice will like the above will only just start two separate activities. So, how can I transfer the values simultaneously?

I'm still pretty new to Android development and also OOP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回忆凄美了谁 2025-01-14 15:50:30

您可以添加多次调用 putExtra 来实现相同的意图:

    Intent requestLink = new Intent(Search.this, Results.class);
    requestLink.putExtra("Link", sendLink);
    requestLink .putExtra("Search", addressInput);
    startActivity(requestLink);

You can add more than call putExtra more than once for the same intent:

    Intent requestLink = new Intent(Search.this, Results.class);
    requestLink.putExtra("Link", sendLink);
    requestLink .putExtra("Search", addressInput);
    startActivity(requestLink);
所有深爱都是秘密 2025-01-14 15:50:30
btnlogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getBaseContext(),db.class);
             String username=uname.getText().toString();
             String upaswrd=pass.getText().toString();
            // Bundle bundle=new Bundle();

          intent.putExtra(name,username);
          intent.putExtra(paswrd, upaswrd);

          startActivity(intent);
        }
    });


/** Db.class */

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.db);
    Intent intent=getIntent();
    String uname=intent.getStringExtra(Gmail.name);
    String upass=intent.getStringExtra(Gmail.paswrd);
    username=(TextView)findViewById(R.id.u);
    username.setText(uname);
    pass=(TextView)findViewById(R.id.p);
    pass.setText(upass);

}
btnlogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent=new Intent(getBaseContext(),db.class);
             String username=uname.getText().toString();
             String upaswrd=pass.getText().toString();
            // Bundle bundle=new Bundle();

          intent.putExtra(name,username);
          intent.putExtra(paswrd, upaswrd);

          startActivity(intent);
        }
    });


/** Db.class */

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.db);
    Intent intent=getIntent();
    String uname=intent.getStringExtra(Gmail.name);
    String upass=intent.getStringExtra(Gmail.paswrd);
    username=(TextView)findViewById(R.id.u);
    username.setText(uname);
    pass=(TextView)findViewById(R.id.p);
    pass.setText(upass);

}
缪败 2025-01-14 15:50:30

您可以使用 Bundle 在 Activity 之间发送数据。
例如,

Intent requestLink = new Intent(Search.this, Results.class);

Bundle bun = new Bundle();
bun.putString("Link",sendLink);
bun.putString("Search", addressInput);

requestLink.putExtras(bun);
startActivity(requestLink);

查看 Bundle api 文档此处

You can use Bundle for sending data between your Activities.
e.g

Intent requestLink = new Intent(Search.this, Results.class);

Bundle bun = new Bundle();
bun.putString("Link",sendLink);
bun.putString("Search", addressInput);

requestLink.putExtras(bun);
startActivity(requestLink);

Check Bundle api documentation here

嘿哥们儿 2025-01-14 15:50:30

只需将两个字符串放在相同的意图中即可。

Intent intent = new Intent(Search.this, Results.class);
intent.putExtra("Link", sendLink);
intent.putExtra("Search", addressInput);

startActivity(intent);

Just put both Strings in the same intent.

Intent intent = new Intent(Search.this, Results.class);
intent.putExtra("Link", sendLink);
intent.putExtra("Search", addressInput);

startActivity(intent);
单调的奢华 2025-01-14 15:50:30
Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link1", sendLink1);
requestLink.putExtra("Link2", sendLink2);
startActivity(requestLink);

//Second Activity 

Bundle bundle=getIntent().getExtras();
String Link1 =bundle.getString("Link1");
String Link2 =bundle.getString("Link2");

根据需要,bundle.get... 有许多重载,例如 getInt,...。

Intent requestLink = new Intent(Search.this, Results.class);
requestLink.putExtra("Link1", sendLink1);
requestLink.putExtra("Link2", sendLink2);
startActivity(requestLink);

//Second Activity 

Bundle bundle=getIntent().getExtras();
String Link1 =bundle.getString("Link1");
String Link2 =bundle.getString("Link2");

bundle.get... has many overloads like getInt,... depending on the need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文