Projekt Schafkopfpunktezähler: Erster Prototyp einer App, die dem Benutzer die Notwendigkeit des Punktezählens beim Schafkopfspiel abnimmt.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChooseGame.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package project.schafkopfzaehler_2;
  2. import android.content.Intent;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.RadioButton;
  9. import android.widget.RadioGroup;
  10. import android.widget.TextView;
  11. public class ChooseGame extends AppCompatActivity {
  12. RadioGroup style, call, soloPlay; // Radio groups init
  13. RadioButton heart; // Heart button init for first invisibility
  14. Button confirmChoice; // Confirm button init
  15. TextView choice; // Choice view init for first choice
  16. String playStyle, playCall, playerName; // Init for choice manipulation
  17. private String names[] = {"", "", "", ""}; // Player names init
  18. private RadioGroup.OnCheckedChangeListener startChangeListener = new RadioGroup.OnCheckedChangeListener() {
  19. @Override
  20. public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
  21. if (radioGroup == style) {
  22. playStyle = getPlayStyle(checkedId);
  23. playCall = getPlayCall(call.getCheckedRadioButtonId());
  24. playerName = getSoloPlayer(soloPlay.getCheckedRadioButtonId());
  25. } else if (radioGroup == call) {
  26. playStyle = getPlayStyle(style.getCheckedRadioButtonId());
  27. playCall = getPlayCall(checkedId);
  28. playerName = getSoloPlayer(soloPlay.getCheckedRadioButtonId());
  29. } else if (radioGroup == soloPlay) {
  30. playStyle = getPlayStyle(style.getCheckedRadioButtonId());
  31. playCall = getPlayCall(call.getCheckedRadioButtonId());
  32. playerName = getSoloPlayer(checkedId);
  33. }
  34. configureChoice(playStyle, playCall, playerName);
  35. }
  36. };
  37. private View.OnClickListener startClickListener = new View.OnClickListener() {
  38. @Override
  39. public void onClick (View v) {
  40. if ( v == confirmChoice ) {
  41. // Send choice to the previous activity
  42. // and then finishes the current activity
  43. Intent data = new Intent();
  44. data.putExtra("choice", choice.getText().toString());
  45. setResult(RESULT_OK, data);
  46. finish();
  47. }
  48. }
  49. };
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.choosegame);
  54. // Get player names from the activity that started this one
  55. Intent intent = getIntent();
  56. names = intent.getStringArrayExtra("playerNames");
  57. // Fill in the player names in the solo choice
  58. soloPlay = findViewById(R.id.soloplay);
  59. for (int i = 0; i < soloPlay.getChildCount(); i++) {
  60. ((RadioButton) soloPlay.getChildAt(i)).setText(names[i]);
  61. }
  62. // Start click listener on confirm button
  63. confirmChoice = findViewById(R.id.gameChoosed);
  64. confirmChoice.setOnClickListener(startClickListener);
  65. // Assign variables radio group and start the change listener
  66. style = findViewById(R.id.playstyle);
  67. style.setOnCheckedChangeListener(startChangeListener);
  68. call = findViewById(R.id.calls);
  69. call.setOnCheckedChangeListener(startChangeListener);
  70. soloPlay.setOnCheckedChangeListener(startChangeListener);
  71. // At first Sauspiel & Eichel is checked, that means that the
  72. // heart can not be called and no solo is played. Therefore
  73. // both options has to be invisible at first
  74. heart = findViewById(R.id.herz);
  75. heart.setVisibility(View.GONE);
  76. TextView titleSolo = findViewById(R.id.title_soloplayer);
  77. titleSolo.setVisibility(View.GONE);
  78. soloPlay.setVisibility(View.GONE);
  79. choice = findViewById(R.id.choice);
  80. choice.setText(getString(R.string.sauspiel1));
  81. }
  82. private String getPlayStyle (int checked) {
  83. // Certain play styles do not allow a call
  84. // In these cases the call view should be invisible
  85. TextView titleCall = findViewById(R.id.title_playcall);
  86. TextView titleSolo = findViewById(R.id.title_soloplayer);
  87. // Return value
  88. String returnStyle = "";
  89. // Find which radio button is selected
  90. switch (checked) {
  91. case R.id.sauspiel:
  92. // Sauspiel
  93. // Toggle visibility
  94. titleCall.setVisibility(View.VISIBLE);
  95. titleCall.setText(getString(R.string.call));
  96. call.setVisibility(View.VISIBLE);
  97. heart.setVisibility(View.GONE);
  98. titleSolo.setVisibility(View.GONE);
  99. soloPlay.setVisibility(View.GONE);
  100. // -----------------
  101. Log.d("ChooseGame", "Sauspiel selected");
  102. returnStyle = "Sauspiel";
  103. break;
  104. case R.id.wenz:
  105. // Wenz
  106. // Toggle visibility
  107. titleCall.setVisibility(View.GONE);
  108. call.setVisibility(View.GONE);
  109. titleSolo.setVisibility(View.GONE);
  110. soloPlay.setVisibility(View.GONE);
  111. // -----------------
  112. Log.d("ChooseGame", "Wenz selected");
  113. returnStyle = "Wenz";
  114. break;
  115. case R.id.solo:
  116. // Solo
  117. // Toggle visibility
  118. titleCall.setVisibility(View.VISIBLE);
  119. titleCall.setText(getString(R.string.soloCall));
  120. call.setVisibility(View.VISIBLE);
  121. titleSolo.setVisibility(View.VISIBLE);
  122. soloPlay.setVisibility(View.VISIBLE);
  123. heart.setVisibility(View.VISIBLE);
  124. // -----------------
  125. Log.d("ChooseGame", "Solo selected");
  126. returnStyle = "Solo";
  127. break;
  128. default:
  129. Log.d("ChooseGame", "!! ERROR - Nothing is selected");
  130. break;
  131. }
  132. return returnStyle;
  133. }
  134. private String getPlayCall (int checked) {
  135. Log.d("ChooseGame", "Call changed");
  136. // Return value
  137. String returnCall = "";
  138. switch (checked) {
  139. case R.id.eichel:
  140. Log.d("ChooseGame", "Eichel selected");
  141. returnCall = "Eichel";
  142. break;
  143. case R.id.blatt:
  144. Log.d("ChooseGame", "Blatt selected");
  145. returnCall = "Blatt";
  146. break;
  147. case R.id.schelle:
  148. Log.d("ChooseGame", "Schelle selected");
  149. returnCall = "Schelle";
  150. break;
  151. case R.id.herz:
  152. Log.d("ChooseGame", "Herz selected");
  153. returnCall = "Herz";
  154. break;
  155. }
  156. return returnCall;
  157. }
  158. private String getSoloPlayer (int checked) {
  159. Log.d("ChooseGame", "Name changed");
  160. // Return value
  161. String name = "";
  162. switch (checked) {
  163. case R.id.player1:
  164. name = ((RadioButton) soloPlay.getChildAt(0)).getText().toString();
  165. break;
  166. case R.id.player2:
  167. name = ((RadioButton) soloPlay.getChildAt(1)).getText().toString();
  168. break;
  169. case R.id.player3:
  170. name = ((RadioButton) soloPlay.getChildAt(2)).getText().toString();
  171. break;
  172. case R.id.player4:
  173. name = ((RadioButton) soloPlay.getChildAt(3)).getText().toString();
  174. break;
  175. default:
  176. // Do nothing
  177. break;
  178. }
  179. return name;
  180. }
  181. private void configureChoice (String style, String call, String name) {
  182. if ( style.equals("Wenz") ) {
  183. choice.setText(style);
  184. } else if ( style.equals("Sauspiel") ) {
  185. switch (call) {
  186. case "Eichel":
  187. choice.setText(getString(R.string.sauspiel1));
  188. break;
  189. case "Blatt":
  190. choice.setText(getString(R.string.sauspiel2));
  191. break;
  192. case "Schelle":
  193. choice.setText(getString(R.string.sauspiel3));
  194. break;
  195. }
  196. } else if ( style.equals("Solo") ) {
  197. switch (call) {
  198. case "Eichel":
  199. choice.setText(getString(R.string.solo1));
  200. choice.append(" " + name);
  201. break;
  202. case "Blatt":
  203. choice.setText(getString(R.string.solo2));
  204. choice.append(" " + name);
  205. break;
  206. case "Schelle":
  207. choice.setText(getString(R.string.solo3));
  208. choice.append(" " + name);
  209. break;
  210. case "Herz":
  211. choice.setText(getString(R.string.solo4));
  212. choice.append(" " + name);
  213. break;
  214. }
  215. }
  216. }
  217. }