Posted By

on 04/06/10


Tagged


Versions (?)

Sudoku (Keypad.java)


 / Published in: Java
 

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

  1. package org.example.sudoku;
  2.  
  3. import android.app.Dialog;
  4. import android.content.Context;
  5. import android.os.Bundle;
  6. import android.view.KeyEvent;
  7. import android.view.View;
  8.  
  9. public class Keypad extends Dialog {
  10. protected static final String TAG = "Sudoku";
  11.  
  12. private final View keys[] = new View[9];
  13. private View keypad;
  14.  
  15. private final int useds[];
  16. private final PuzzleView puzzleView;
  17.  
  18. public Keypad(Context context, int useds[], PuzzleView puzzleView) {
  19. super(context);
  20. this.useds = useds;
  21. this.puzzleView = puzzleView;
  22. }
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27.  
  28. setTitle(R.string.keypad_title);
  29. setContentView(R.layout.keypad);
  30. findViews();
  31. for(int element : useds) {
  32. if(element != 0)
  33. keys[element - 1].setVisibility(View.INVISIBLE);
  34. }
  35. setListeners();
  36. }
  37.  
  38. private void findViews() {
  39. keypad = findViewById(R.id.keypad);
  40. keys[0] = findViewById(R.id.keypad_1);
  41. keys[1] = findViewById(R.id.keypad_2);
  42. keys[2] = findViewById(R.id.keypad_3);
  43. keys[3] = findViewById(R.id.keypad_4);
  44. keys[4] = findViewById(R.id.keypad_5);
  45. keys[5] = findViewById(R.id.keypad_6);
  46. keys[6] = findViewById(R.id.keypad_7);
  47. keys[7] = findViewById(R.id.keypad_8);
  48. keys[8] = findViewById(R.id.keypad_9);
  49. }
  50.  
  51. private void setListeners() {
  52. for(int i = 0; i < keys.length; i++) {
  53. final int t = i + 1;
  54. keys[i].setOnClickListener(new View.OnClickListener() {
  55. public void onClick(View v) {
  56. returnResult(t);
  57. }});
  58. }
  59. keypad.setOnClickListener(new View.OnClickListener() {
  60. public void onClick(View v) {
  61. returnResult(0);
  62. }});
  63. }
  64.  
  65. @Override
  66. public boolean onKeyDown(int keyCode, KeyEvent event) {
  67. int tile = 0;
  68. switch(keyCode) {
  69. case KeyEvent.KEYCODE_0:
  70. case KeyEvent.KEYCODE_SPACE: tile = 0; break;
  71. case KeyEvent.KEYCODE_1: tile = 1; break;
  72. case KeyEvent.KEYCODE_2: tile = 2; break;
  73. case KeyEvent.KEYCODE_3: tile = 3; break;
  74. case KeyEvent.KEYCODE_4: tile = 4; break;
  75. case KeyEvent.KEYCODE_5: tile = 5; break;
  76. case KeyEvent.KEYCODE_6: tile = 6; break;
  77. case KeyEvent.KEYCODE_7: tile = 7; break;
  78. case KeyEvent.KEYCODE_8: tile = 8; break;
  79. case KeyEvent.KEYCODE_9: tile = 9; break;
  80. default:
  81. return super.onKeyDown(keyCode, event);
  82. }
  83. if(isValid(tile)) {
  84. returnResult(tile);
  85. }
  86. return true;
  87. }
  88.  
  89. private boolean isValid(int tile) {
  90. for(int t : useds) {
  91. if(tile == t) return false;
  92. }
  93. return true;
  94. }
  95.  
  96. /** Return the chosen tile to the caller */
  97. private void returnResult(int tile) {
  98. puzzleView.setSelectedTile(tile);
  99. dismiss();
  100. }
  101. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: ziye on April 7, 2010

The clock in the high church tower struck and the sound made him remember his parents'wow gold sale early love

for him. They had taught him and prayed to God for his good.wow accounts But he chose the wrong way. wow account With shame and glonger look towards that .ghd straighteners His

darkened eyes were full of tears, LVand with a despairing effort, he burst out a cry: "Come back, my early days! Come back!" yy2010.4.7

Posted By: rubioberry on June 20, 2012

When i copy paste this into eclipse, why does it show error on lines 28 ( keypad title cannot be resolved ) Also lines 39 though 48? ( keypad cannot be resolved or is not a filed ) Have I missed something?

You need to login to post a comment.