Advertisement
mmayoub

MyCounterApplicatin, MainActivity.java

Sep 9th, 2022
948
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | Software | 0 0
  1. package com.example.mycounterapplication;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9.  
  10.  
  11. public class MainActivity extends AppCompatActivity {
  12.     // 1 - define variables
  13.     private TextView tvMyCounter;
  14.     private Button btnAdd, btnMinus, btnRestart, btnQuit;
  15.  
  16.     private int count = 0;
  17.  
  18.     @Override
  19.     protected void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22.  
  23.         // 2 - set layout connection to local variables
  24.         tvMyCounter = findViewById(R.id.tvCounter);
  25.         btnAdd = findViewById(R.id.btnAdd);
  26.         btnMinus = findViewById(R.id.btnMinus);
  27.         btnRestart = findViewById(R.id.btnRestart);
  28.         btnQuit = findViewById(R.id.btnQuit);
  29.  
  30.         // 3 - test
  31.         tvMyCounter.setText("0");
  32.     }
  33.  
  34.     public void exit(View view) {
  35.         finish();
  36.     }
  37.  
  38.     public void add(View view) {
  39.         count++;
  40.         tvMyCounter.setText(count + "");
  41.     }
  42.  
  43.     public void minus(View view) {
  44.         if (count > 0) {
  45.             count--;
  46.             tvMyCounter.setText(count + "");
  47.         }
  48.     }
  49.  
  50.     public void restart(View view) {
  51.         count = 0;
  52.         tvMyCounter.setText("0");
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement