Advertisement
Guest User

DisplayPlayersActivity.java

a guest
May 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. package com.example.scorekeeper;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.TextView;
  9.  
  10.  
  11.  
  12. public class DisplayPlayersActivity extends AppCompatActivity {
  13.  
  14. public int scorePlayer1 = 0;
  15. public int scorePlayer2 = 9;
  16. public int scorePlayer3 = 50;
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_display_players);
  22.  
  23. // Get the intent that started the activity and extract the string
  24. Intent intent = getIntent();
  25. String name1 = intent.getStringExtra(MainActivity.PLAYER_NAME1);
  26. String name2 = intent.getStringExtra(MainActivity.PLAYER_NAME2);
  27. String name3 = intent.getStringExtra(MainActivity.PLAYER_NAME3);
  28.  
  29. // Capture the layout's TextView and set the string as its text
  30. TextView textView1 = findViewById(R.id.playerName1);
  31. TextView textView2 = findViewById(R.id.playerName2);
  32. TextView textView3 = findViewById(R.id.playerName3);
  33.  
  34. textView1.setText(name1);
  35. textView2.setText(name2);
  36. textView3.setText(name3);
  37.  
  38. // Display players scores
  39. setScorePlayer1(scorePlayer1);
  40. setScorePlayer2(scorePlayer2);
  41. setScorePlayer3(scorePlayer3);
  42.  
  43. }
  44.  
  45. public void setScorePlayer1(int scorePlayer1) {
  46. TextView textViewScorePlayer1 = findViewById(R.id.totalScorePlayer1);
  47. String scorePlayer1String = String.format("%03d", scorePlayer1);
  48. textViewScorePlayer1.setText(scorePlayer1String);
  49. }
  50.  
  51. public void setScorePlayer2(int scorePlayer2) {
  52. TextView textViewScorePlayer2 = findViewById(R.id.totalScorePlayer2);
  53. String scorePlayer2String = String.format("%03d", scorePlayer2);
  54. textViewScorePlayer2.setText(scorePlayer2String);
  55. }
  56.  
  57. public void setScorePlayer3(int scorePlayer3) {
  58. TextView textViewScorePlayer3 = findViewById(R.id.totalScorePlayer3);
  59. String scorePlayer3String = String.format("%03d", scorePlayer3);
  60. textViewScorePlayer3.setText(scorePlayer3String);
  61. }
  62.  
  63. public void incScorePlayer1(View view) {
  64. this.scorePlayer1++;
  65. setScorePlayer1(scorePlayer1);
  66. }
  67.  
  68. public void decScorePlayer1(View view) {
  69. this.scorePlayer1--;
  70. setScorePlayer1(scorePlayer1);
  71. }
  72.  
  73. public void incScorePlayer2(View view) {
  74. this.scorePlayer2++;
  75. setScorePlayer2(scorePlayer2);
  76. }
  77.  
  78. public void decScorePlayer2(View view) {
  79. this.scorePlayer2--;
  80. setScorePlayer2(scorePlayer2);
  81. }
  82.  
  83. public void incScorePlayer3(View view) {
  84. this.scorePlayer3++;
  85. setScorePlayer3(scorePlayer3);
  86. }
  87.  
  88. public void decScorePlayer3(View view) {
  89. this.scorePlayer3--;
  90. setScorePlayer3(scorePlayer3);
  91. }
  92.  
  93. public void clearAllScores (View view) {
  94. this.scorePlayer1 = 0;
  95. this.scorePlayer2 = 0;
  96. this.scorePlayer3 = 0;
  97. setScorePlayer1(scorePlayer1);
  98. setScorePlayer2(scorePlayer2);
  99. setScorePlayer3(scorePlayer3);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement