java - 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
解决方案
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("playerOne", player1Name);
intent.putExtra("playerTwo", player2Name);
startActivity(intent);
您可以将玩家名称从第一个活动传递到下一个活动。这可能会对您有所帮助。
推荐阅读
- python - 有没有一种方法可以将整个 jupyter 笔记本的输出保存到 txt 文件或类似文件中,而不使用命令行,而是从笔记本内部?
- selenium - 如何在运行中从 Perfecto 中选择可用设备
- angular - 创建 observable 以返回 csv 值
- rest - 如何使用 softlayer Rest API 在 IBM 云中使用数据磁盘启动计算实例
- installation - Sas Add-in for Microsoft office, installation error in office 365 / windows10 : Culture is not supported
- javascript - 您如何在 cucumber-js 测试之间重用 selenium webdriver 浏览器实例?
- c - UART PIC24 - 接收连续的 ASCII 字符流
- wordpress - 重力形式填充地址和时间字段
- scala - zipWithIndex 从 1 而不是 0
- woocommerce-subscriptions - WooCommerce 订阅:如何确定给定订阅的最后一个正确支付的订单