当我运行它时,我得到了两个TextView中都为“ name2”输入的内容
这是因为您要
Activity使用第二个实例创建一个新的实例
Intent。您可以通过不同的方式来做到这一点。一个方法是创建一个
Intent作为成员变量的单变量,在您的第一个函数调用中将其实例化,添加额外功能,然后在第二个方法中添加另一个额外功能,然后在
startActivity此处调用。
但是同时进行所有操作可能会更容易且更具可读性。
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);只需调用此方法即可。
然后用
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 ;


