开发者

code for TouchPad works, but not for DPAD ...please help me to fix this

package org.coe.twoD;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint; //import android.graphics.Path;
import android.graphics.Rect; //import android.graphics.RectF;
import android.os.Bundle; //import android.util.Log;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import android.view.View.OnClickListener;

public class TwoD extends Activity implements OnClickListener {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  View draw2d = findViewById(R.id.draw_button);
  draw2d.setOnClickListener(this);

 }

 public void onClick(View v) {

  if (R.id.draw_button == v.getId()) {

   setContentView(new draw2D(this));

  }
 }

 public class draw2D extends View {
  private static final String TAG = "Sudoku";

  private float width; // width of one tile
  private float height; // height of one tile
  private int selX; // X index of selection
  private int selY; // Y index of selection
  private final Rect selRect = new Rect();

  public draw2D(Context context) {
       super(context);

  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   width = w / 9f;
   height = h / 9f;
   getRect(selX, selY, selRect);
   Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
   super.onSizeChanged(w, h, oldw, oldh);
  }

  @Override
  protected void onDraw(Canvas canvas) {
   // Draw the background...
   Paint background = new Paint();
   background.setColor(getResources().getColor(R.color.background));
   canvas.drawRect(0, 0, getWidth(), getHeight(), background);

   // Draw the board...

   // Define colors for the grid lines
   Paint dark = new Paint();
   dark.setColor(getResources().getColor(R.color.dark));

   Paint hilite = new Paint();
   hilite.setColor(getResources().getColor(R.color.hilite));

   Paint light = new Paint();
   light.setColor(getResources().getColor(R.color.light));

   // Draw the minor grid lines
   for (int i = 0; i < 9; i++) {
    canvas.drawLine(0, i * height, getWidth(), i * height, light);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
  开发者_C百科    hilite);
    canvas.drawLine(i * width, 0, i * width, getHeight(), light);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
      hilite);
   }

   // Draw the major grid lines
   for (int i = 0; i < 9; i++) {
    if (i % 3 != 0)
     continue;
    canvas.drawLine(0, i * height, getWidth(), i * height, dark);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
      hilite);
    canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
    canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
      hilite);
   }

   /*
    * dark.setColor(Color.MAGENTA); Path circle= new Path();
    * circle.addCircle(150, 150, 100, Path.Direction.CW);
    * canvas.drawPath(circle, dark);
    * 
    * 
    * Path rect=new Path();
    * 
    * RectF rectf= new RectF(150,200,250,300); rect.addRect(rectf,
    * Path.Direction.CW); canvas.drawPath(rect, dark);
    * 
    * 
    * canvas.drawRect(0, 0,250, 250, dark);
    * 
    * 
    * canvas.drawText("Hello", 200,200, dark);
    */
   Paint selected = new Paint();
   selected.setColor(Color.GREEN);
   canvas.drawRect(selRect, selected);

  }

  /*
   * public boolean onTouchEvent(MotionEvent event){
   * if(event.getAction()!=MotionEvent.ACTION_DOWN) return
   * super.onTouchEvent(event);
   * select((int)(event.getX()/width),(int)(event.getY()/height));
   * 
   * 
   * return true; }
   */

  private void select(int x, int y) {
   invalidate(selRect);
   selX = Math.min(Math.max(x, 0), 8);
   selY = Math.min(Math.max(y, 0), 8);
   getRect(selX, selY, selRect);
   invalidate(selRect);

  }
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
   return super.onKeyUp(keyCode, event);
  }



 @Override
 public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN)
       return super.onTouchEvent(event);

    select((int) (event.getX() / width),
          (int) (event.getY() / height));
   // game.showKeypadOrError(selX, selY);
    Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
    return true;
 }



 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event="
          + event);
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_UP:
       select(selX, selY - 1);
       break;
    case KeyEvent.KEYCODE_DPAD_DOWN:
       select(selX, selY + 1);
       break;
    case KeyEvent.KEYCODE_DPAD_LEFT:
       select(selX - 1, selY);
       break;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
       select(selX + 1, selY);
       break;
      default:
       return super.onKeyDown(keyCode, event);
    }
    return true;
 }

  private void getRect(int x, int y, Rect rect) {
   rect.set((int) (x * width), (int) (y * height),
     (int) (x * width + width), (int) (y * height + height));
  }
 }

}


I forgot to add follwing codes:

this.requestFocus(); this.setFocusableInTouchMode(true);

Once i have done that, rest was as expected!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜