如何获取在Android中按下的键盘按键的X和Y坐标

时间:2018-11-18 19:11:12

标签: android

private View.OnKeyListener handleTouch = new View.OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {

        int x = (int) v.getX();
        int y = (int) v.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.i("TAG", "touched down");
// Toast.makeText(MainActivity.this, "touched down", Toast.LENGTH_SHORT).show(); cordinate.setText("touched down");

                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("TAG", "moving: (" + x + ", " + y + ")");
// Toast.makeText(MainActivity.this, "moving: (X" + x + ", Y" + y + ")", Toast.LENGTH_SHORT).show(); cordinate.setText("X" + x + ", Y" + y + "");

                break;
            case MotionEvent.ACTION_UP:
                Log.i("TAG", "touched up");
// Toast.makeText(MainActivity.this, "touched up", Toast.LENGTH_SHORT).show(); cordinate.setText("touched up"); 
                break; 
        }

        return true;
    }
};

0 个答案:

没有答案