Advertisement
Shiyan12

Menu.java

Jul 19th, 2021
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. package com.example.youdothemath;
  2.  
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.Button;
  7. import android.app.AlertDialog;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10.  
  11. public class Menu extends MainActivity implements OnClickListener {
  12.     private Button playBtn, helpBtn, highBtn;
  13.     private String[] levelNames = {"Easy", "Medium", "Hard"};
  14.     @Override
  15.     protected void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.activity_menu);
  18.         playBtn = (Button)findViewById(R.id.play_btn);
  19.         helpBtn = (Button)findViewById(R.id.help_btn);
  20.         highBtn = (Button)findViewById(R.id.high_btn);
  21.         playBtn.setOnClickListener(this);
  22.         helpBtn.setOnClickListener(this);
  23.         highBtn.setOnClickListener(this);
  24.  
  25.     }
  26.     @Override
  27.     public void onClick(View view) {
  28.         if (view.getId() == R.id.play_btn) {
  29.             // play button
  30.             AlertDialog.Builder builder = new AlertDialog.Builder(this);
  31.             builder.setTitle("Choose a level")
  32.                     .setSingleChoiceItems(levelNames, 0, new DialogInterface.OnClickListener() {
  33.                         public void onClick(DialogInterface dialog, int which) {
  34.                             dialog.dismiss();
  35.                             //start gameplay
  36.                             startPlay(which);
  37.                         }
  38.                     });
  39.             AlertDialog ad = builder.create();
  40.             ad.show();
  41.  
  42.         } else if (view.getId() == R.id.help_btn) {
  43.             // how to play button
  44.             Intent helpIntent = new Intent(this, HowToPlay.class);
  45.             this.startActivity(helpIntent);
  46.  
  47.         } else if (view.getId() == R.id.high_btn) {
  48.             // high scores button
  49.             Intent highIntent = new Intent(this, HighScores.class);
  50.             this.startActivity(highIntent);
  51.         }
  52.  
  53.     }
  54.     private void startPlay(int chosenLevel) {
  55.         //start gameplay
  56.         Intent playIntent = new Intent(this, PlayGame.class);
  57.         playIntent.putExtra("level", chosenLevel);
  58.         this.startActivity(playIntent);
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement