Я очень новичок в android, и я пытаюсь отправить введенные пользователем данные (их имена) в другое действие. В прошлом я мог отправлять одиночные строки между действиями с помощью Intents, но мне не удалось разобраться, как отправить две разные строки в два разных TextView.Отправка нескольких намерений от одного действия к другому действию
Вот мой код 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);
}
}
Код для моей второй деятельности, 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);
}
}
Когда я запускаю это, я получаю все, что было вложено в течение " name2 'в обоих текстовых окнах. Что мне нужно сделать, чтобы изменить это?
Почему бы не собрать все это и передать его в одной связке с новым видом деятельности? – ObieMD5