Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.android.task3;
- import android.app.*;
- import android.os.*;
- import android.view.*;
- import android.widget.*;
- import java.util.*;
- public class MainActivity extends Activity
- {
- private int number_to_find;
- public int number_from_player;
- private int points;
- public void clickButton1(View view) {
- check(number_to_find, number_from_player);
- }
- private void check(int number_to_find, int number_from_player) {
- Button check = (Button) findViewById(R.id.buttonRight);
- Button given_number = (Button) findViewById(R.id.buttonGiven);
- Button generate_number = (Button) findViewById(R.id.buttonGenerate);
- EditText get_number_field = (EditText) findViewById(R.id.edittext);
- generate_number.setText("Generate new number");
- generate_number.setVisibility(View.GONE);
- number_from_player = Integer.parseInt(get_number_field.getText().toString());
- given_number.setText("Your number " + number_from_player);
- boolean won = false;
- if(number_to_find > number_from_player)
- {
- check.setText("Too less");
- points--;
- }
- else if(number_to_find < number_from_player)
- {
- check.setText("Too much");
- points--;
- }
- else
- {
- check.setText("You won!!! Hurray");
- won=true;
- points+=10;
- }
- if(won==true)
- {
- generate_number.setVisibility(View.VISIBLE);
- generateNumber();
- }
- TextView pointsView = (TextView) findViewById(R.id.pointsTextView);
- pointsView.setText("Points: " + points);
- }
- //method to generate number
- private void generateNumber()
- {
- Button ok = (Button) findViewById(R.id.buttonLeft);
- Random generator = new Random();
- number_to_find = generator.nextInt(100);
- ok.setText("OK");
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- generateNumber();
- }
- }
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0"
- android:id="@+id/edittext"
- android:inputType="phone"
- android:digits="1234567890" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:id="@+id/buttonGiven"
- android:layout_centerVertical="false"
- android:layout_alignParentRight="true"
- android:textSize="40dp" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:id="@+id/buttonLeft"
- android:layout_centerVertical="true"
- android:layout_alignParentLeft="true"
- android:textSize="40dp"
- android:onClick="clickButton1" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text=""
- android:id="@+id/buttonGenerate"
- android:layout_centerVertical="true"
- android:layout_alignParentBottom="true"
- android:textSize="40dp"
- android:onClick="clickButton1" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="0"
- android:id="@+id/buttonRight"
- android:layout_centerVertical="true"
- android:layout_alignParentRight="true"
- android:textSize="40dp" />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Punkty: 0"
- android:id="@+id/pointsTextView"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true" />
- </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement