首页 > 解决方案 > How to create 2 new Players and get there name from an EditText field

问题描述

I have an app with 2 layouts in android studio. The first had just two EditText boxes and a start button. The two players will put in their names and then press start. This will then take them to the next screen which is the main screen. I want 2 text fields in here to automatically be filled in with the players names that were input on the first page.

I have Main_activity and a Player class. In the player class I have

public Player() {
String name;
int score;

So when a new player is created they get a name and a score.

I've never used classes before and I am trying to change an app I've already made into the same app but using classes (I'm doing a computer science degree and want to learn to do it properly)

I am not really sure what is the correct way to get the players names from the first screen. I know I could just create two variables player1Name player2Name and then set the text in the main text field app to display these 2 variables. But we have recently done some stuff on classes and my understanding is when I do, Player p1 = new Player(); Player p2 = new Player();

each of these instances of Player will get a name variable. I can then use something along the lines of p1.name to access this variable? I am just not fully sure how to assign it,

Would it somehow be done by making another variable which would then be used in

Player p1 = new Player(player1Name);

Sorry if that's confusing! I have confused myself :)

Thanks for an help

标签: javaandroid

解决方案


Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("playerOne", player1Name);
intent.putExtra("playerTwo", player2Name);
startActivity(intent);

您可以将玩家名称从第一个活动传递到下一个活动。这可能会对您有所帮助。


推荐阅读