Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. package com.example.android.task3;
  2.  
  3. import android.app.*;
  4. import android.os.*;
  5. import android.view.*;
  6. import android.widget.*;
  7. import java.util.*;
  8.  
  9. public class MainActivity extends Activity
  10. {
  11. private int number_to_find;
  12. public int number_from_player;
  13. private int points;
  14.  
  15. public void clickButton1(View view) {
  16. check(number_to_find, number_from_player);
  17. }
  18.  
  19. private void check(int number_to_find, int number_from_player) {
  20. Button check = (Button) findViewById(R.id.buttonRight);
  21. Button given_number = (Button) findViewById(R.id.buttonGiven);
  22. Button generate_number = (Button) findViewById(R.id.buttonGenerate);
  23. EditText get_number_field = (EditText) findViewById(R.id.edittext);
  24. generate_number.setText("Generate new number");
  25. generate_number.setVisibility(View.GONE);
  26. number_from_player = Integer.parseInt(get_number_field.getText().toString());
  27. given_number.setText("Your number " + number_from_player);
  28.  
  29. boolean won = false;
  30.  
  31. if(number_to_find > number_from_player)
  32. {
  33. check.setText("Too less");
  34. points--;
  35. }
  36. else if(number_to_find < number_from_player)
  37. {
  38. check.setText("Too much");
  39. points--;
  40. }
  41. else
  42. {
  43. check.setText("You won!!! Hurray");
  44. won=true;
  45. points+=10;
  46. }
  47. if(won==true)
  48. {
  49. generate_number.setVisibility(View.VISIBLE);
  50. generateNumber();
  51. }
  52. TextView pointsView = (TextView) findViewById(R.id.pointsTextView);
  53. pointsView.setText("Points: " + points);
  54. }
  55.  
  56. //method to generate number
  57. private void generateNumber()
  58. {
  59. Button ok = (Button) findViewById(R.id.buttonLeft);
  60. Random generator = new Random();
  61. number_to_find = generator.nextInt(100);
  62. ok.setText("OK");
  63.  
  64. }
  65. /** Called when the activity is first created. */
  66. @Override
  67. public void onCreate(Bundle savedInstanceState)
  68. {
  69. super.onCreate(savedInstanceState);
  70. setContentView(R.layout.main);
  71. generateNumber();
  72. }
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  81. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  82. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  83. android:paddingRight="@dimen/activity_horizontal_margin"
  84. android:paddingTop="@dimen/activity_vertical_margin"
  85. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
  86.  
  87. <EditText
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content"
  90. android:text="0"
  91. android:id="@+id/edittext"
  92. android:inputType="phone"
  93. android:digits="1234567890" />
  94.  
  95. <Button
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:text=""
  99. android:id="@+id/buttonGiven"
  100. android:layout_centerVertical="false"
  101. android:layout_alignParentRight="true"
  102. android:textSize="40dp" />
  103.  
  104.  
  105. <Button
  106. android:layout_width="wrap_content"
  107. android:layout_height="wrap_content"
  108. android:text=""
  109. android:id="@+id/buttonLeft"
  110. android:layout_centerVertical="true"
  111. android:layout_alignParentLeft="true"
  112. android:textSize="40dp"
  113. android:onClick="clickButton1" />
  114.  
  115. <Button
  116. android:layout_width="wrap_content"
  117. android:layout_height="wrap_content"
  118. android:text=""
  119. android:id="@+id/buttonGenerate"
  120. android:layout_centerVertical="true"
  121. android:layout_alignParentBottom="true"
  122. android:textSize="40dp"
  123. android:onClick="clickButton1" />
  124.  
  125.  
  126. <Button
  127. android:layout_width="wrap_content"
  128. android:layout_height="wrap_content"
  129. android:text="0"
  130. android:id="@+id/buttonRight"
  131. android:layout_centerVertical="true"
  132. android:layout_alignParentRight="true"
  133. android:textSize="40dp" />
  134.  
  135. <TextView
  136. android:layout_width="wrap_content"
  137. android:layout_height="wrap_content"
  138. android:text="Punkty: 0"
  139. android:id="@+id/pointsTextView"
  140. android:layout_alignParentBottom="true"
  141. android:layout_centerHorizontal="true" />
  142.  
  143. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement