Posted By

on 03/29/10


Tagged


Versions (?)

Sudoku (Sudoku.java)


 / Published in: Java
 

from "Hello Android 2nd Edition" (ISBN: 1-934356-49-2)

  1. package org.example.sudoku;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.content.Intent;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.Menu;
  9. import android.view.MenuInflater;
  10. import android.view.MenuItem;
  11. import android.app.AlertDialog;
  12. import android.content.DialogInterface;
  13. import android.util.Log;
  14.  
  15. public class Sudoku extends Activity implements OnClickListener {
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21.  
  22. // Set up click listeners for all the buttons
  23. View continueButton = findViewById(R.id.continue_button);
  24. continueButton.setOnClickListener(this);
  25. View newButton = findViewById(R.id.new_button);
  26. newButton.setOnClickListener(this);
  27. View aboutButton = findViewById(R.id.about_button);
  28. aboutButton.setOnClickListener(this);
  29. View exitButton = findViewById(R.id.exit_button);
  30. exitButton.setOnClickListener(this);
  31. }
  32.  
  33. public void onClick(View v) {
  34. switch(v.getId()) {
  35. case R.id.about_button:
  36. Intent i = new Intent(this, About.class);
  37. startActivity(i);
  38. break;
  39. case R.id.new_button:
  40. openNewGameDialog();
  41. break;
  42. case R.id.exit_button:
  43. finish();
  44. break;
  45. }
  46. }
  47.  
  48. @Override
  49. public boolean onCreateOptionsMenu(Menu menu) {
  50. super.onCreateOptionsMenu(menu);
  51. MenuInflater inflater = getMenuInflater();
  52. inflater.inflate(R.menu.menu, menu);
  53. return true;
  54. }
  55.  
  56. @Override
  57. public boolean onOptionsItemSelected(MenuItem item) {
  58. switch (item.getItemId()) {
  59. case R.id.settings:
  60. startActivity(new Intent(this, Prefs.class));
  61. return true;
  62. }
  63. return false;
  64. }
  65.  
  66. private static final String TAG = "Sudoku";
  67.  
  68. /** Ask the user what difficulty level they want */
  69. private void openNewGameDialog() {
  70. new AlertDialog.Builder(this)
  71. .setTitle(R.string.new_game_title)
  72. .setItems(R.array.difficulty,
  73. new DialogInterface.OnClickListener() {
  74. public void onClick(DialogInterface dialoginterface,
  75. int i) {
  76. startGame(i);
  77. }
  78. })
  79. .show();
  80. }
  81.  
  82. /** Start a new game with the given difficulty level */
  83. private void startGame(int i) {
  84. Log.d(TAG, "clicked on " + i);
  85. Intent intent = new Intent(Sudoku.this, Game.class);
  86. intent.putExtra(Game.KEY_DIFFICULTY, i);
  87. startActivity(intent);
  88. }
  89.  
  90. @Override
  91. protected void onResume() {
  92. super.onResume();
  93. Music.play(this, R.raw.main);
  94. }
  95.  
  96. @Override
  97. protected void onPause() {
  98. super.onPause();
  99. Music.stop(this);
  100. }
  101. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: rubioberry on June 20, 2012

how do I resolve the error on line 93 R.raw.main ( raw cannot be resolved or is not a field )?

You need to login to post a comment.