Java 将多个意图从一个活动发送到另一个活动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18195734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:12:11  来源:igfitidea点击:

Sending Multiple Intents from a Single Activity to Another Activity

javaandroidandroid-intentandroid-studio

提问by Sam Borick

I am very new to android, and I am trying to send user-inputted data (their names) to another activity. I have in the past been able to send single lines between activities using Intents, but I have not been able to work out how to send two different strings to two different TextViews.

我对 android 很陌生,我正在尝试将用户输入的数据(他们的名字)发送到另一个活动。我过去曾能够使用 Intent 在活动之间发送单行,但我无法弄清楚如何将两个不同的字符串发送到两个不同的 TextView。

Here is my code for the MainActivity so far:

到目前为止,这是我的 MainActivity 代码:

package com.example.game;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendNames();
            sendNames2();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public void sendNames() {
    //sends player1's name to mainGame
    Intent intent = new Intent (this, MainGame.class);
    EditText player1 = (EditText) findViewById(R.id.player1);
    String message = player1.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}//sends player2's name to mainGame
 public void sendNames2(){
    Intent intent2 = new Intent(this, MainGame.class);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String message2 = player2.getText().toString();
    intent2.putExtra(EXTRA_MESSAGE, message2);
    startActivity(intent2);
}
}

Code for my second activity, MainGame:

我的第二个活动 MainGame 的代码:

package com.example.game;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.annotation.SuppressLint;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
import android.widget.TextView;

public class MainGame extends Activity {
@SuppressLint("NewAPI")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    //retrives player1's name
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView name1 = (TextView) findViewById(R.id.name1);
    name1.setText(message);
    //retrivews player2's name
    Intent intent2 = getIntent();
    String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText(message2);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

       return super.onOptionsItemSelected(item);
}

}

When I run this, I get whatever has been put in for 'name2' in both TextViews. What do I need to do to change this?

当我运行它时,我得到了在两个 TextViews 中为 'name2' 设置的任何内容。我需要做什么来改变这种情况?

采纳答案by codeMagic

when i run this i get whatever has been put in for 'name2' in both TextView's

当我运行它时,我得到了在两个 TextView 中为“name2”添加的任何内容

This is because you are creating a new instance of the Activitywith the second Intent. There are different ways you could do it. One would be create a single Intentas a member variable, instantiate it in your first function call, add extras, then add the other extra in the second method, and call startActivitythere.

这是因为您正在Activity使用第二个Intent. 你可以用不同的方法来做到这一点。一种方法是创建一个Intent作为成员变量的单个变量,在您的第一个函数调用中实例化它,添加额外内容,然后在第二个方法中添加另一个额外内容,并startActivity在那里调用。

But it would probably be easier and more readable to just do it all at the same time.

但是,同时执行所有操作可能会更容易且更具可读性。

 public void sendNames() {
    //sends player1's name to mainGame
    Intent intent = new Intent (this, MainGame.class);
    EditText player1 = (EditText) findViewById(R.id.player1);
    String player1Name= player1.getText().toString();
    intent.putExtra("player1Name", player1Name);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String player2Name= player2.getText().toString();
    intent2.putExtra("player2Name", player2Name);
    startActivity(intent);

And just call this one method.

只需调用这一种方法。

Then get it with

然后得到它

Intent intent = getIntent();
String name1 = intent.getStringExtra("player1Name");
TextView name1 = (TextView) findViewById(R.id.name1);
name1.setText(message);

String name1 = intent2.getStringExtra("player2Name");
TextView name2 = (TextView) findViewById(R.id.name2);
name2.setText(name1 ;

回答by ben75

An Intent is like a Map where you can store String (and other serializable objects) with a key. In your code you are using the same key EXTRA_MESSAGEtwice.

Intent 就像一个 Map,您可以在其中使用键存储 String(和其他可序列化对象)。在您的代码中,您EXTRA_MESSAGE两次使用相同的密钥。

Also, keep in mind that you use one Intent to start one Activity: you CANNOT have 2 Intent starting a single instance of an Activity. The solution is to put the 2 values with 2 distinct keys in one Intent.

另外,请记住,您使用一个 Intent 来启动一个 Activity:您不能有 2 个 Intent 来启动一个 Activity 的单个实例。解决方案是将具有 2 个不同键的 2 个值放在一个 Intent 中。

回答by Sunil Mishra

You can send multiple paramters to an activity using intent, You don't have to create two different intent for doing this.

您可以使用意图将多个参数发送到活动,您不必为此创建两个不同的意图。

You can do this as follows

您可以按如下方式执行此操作

public void sendNames() {
    Intent intent = new Intent (this, MainGame.class);
    EditText player1 = (EditText) findViewById(R.id.player1);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String message = player1.getText().toString();
    String message2 = player2.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    intent2.putExtra(EXTRA_MESSAGE1, message2);
    startActivity(intent);
}

and then get those values like

然后得到这些值

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE1);
TextView name1 = (TextView) findViewById(R.id.name1);
name1.setText(message);
TextView name2 = (TextView) findViewById(R.id.name2);
name2.setText(message2);