【问题标题】:Retrieve X and Y coordinates of a key in android在android中检索一个键的X和Y坐标
【发布时间】:2015-06-11 17:03:36
【问题描述】:

我正在使用 android 的自定义键盘来构建一个小键盘。 android 中的 OnKeyboardActionListener 返回被按下的键的“keyCode”。但我特别想要该键的屏幕 x 和 y 坐标(使用或不使用 keyCode )。

P.S:即使是键中心的坐标也会有所帮助。我找到了一个方法“squaredDistanceFrom”,它返回键中心和屏幕被触摸点之间的平方距离。有没有返回中心坐标的函数?

【问题讨论】:

    标签: android-keypad


    【解决方案1】:

    我知道这个问题现在已经很老了,但是在过去的两周里,我一直在从事一个具有类似要求的项目,但我一直在努力寻找解决方案。所以这是我的建议:

    构建一个工作键盘

    首先,让我们从功能键盘开始。一些非常有用的入门资源:

    获取按键的坐标

    我还没有找到从 API 中按下的键检索坐标的直接方法。

    但是,我们可以确定并检索按下的键的实际实例。首先我们需要在 KeyboardView 上创建一个onTouchListener。这将允许我们检索MotionEvent,从而检索视图中触摸事件的坐标。

    然后,Keyboard 类中有一个名为getKeys 的方法返回Keys 的列表。然后,我们可以使用Key 类中的isInside 方法来验证之前检索到的坐标是否在键内,从而检索按下的键的实例。

    每次显示时,我们都需要从当前键盘检索按键列表。这样,即使用户从一种布局切换到另一种布局(例如通过使用符号或数字),我们也可以获得正确的键集。我们可以通过覆盖我们类中的onStartInputView 方法来做到这一点。

    样本

    检索密钥:

    public void retrieveKeys() {
        keyList = kv.getKeyboard().getKeys();
    }
    

    覆盖显示键盘时调用的onStartInputView:

    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {
        super.onStartInputView(info, restarting);
        retrieveKeys();
    }
    

    为 KeyboardView 创建一个 onTouchListener:

    @Override
    public View onCreateInputView() {
        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        kv.setOnKeyboardActionListener(this);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
    
        retrieveKeys();
    
        // Set the onTouchListener to be able to retrieve a MotionEvent
        kv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // For each key in the key list
                for (Keyboard.Key k : keyList) {
                    // If the coordinates from the Motion event are inside of the key
                    if (k.isInside((int) event.getX(), (int) event.getY())) {
                        // k is the key pressed
                        Log.d("Debugging",
                                "Key pressed: X=" + k.x + " - Y=" + k.y);
                        int centreX, centreY;
                        centreX = (k.width/2) + k.x;
                        centreY = (k.width/2) + k.x;
                        // These values are relative to the Keyboard View
                        Log.d("Debugging",
                                "Centre of the key pressed: X="+centreX+" - Y="+centreY);
                    }
                }
                // Return false to avoid consuming the touch event
                return false;
            }
        });
    
        return kv;
    }
    

    整个示例(不包括包和导入):

    public class MyTestKeyboard extends InputMethodService
            implements KeyboardView.OnKeyboardActionListener {
    
        private KeyboardView kv;
        private Keyboard keyboard;
        private boolean caps = false;
    
        private List<Keyboard.Key> keyList;
    
        public void retrieveKeys() {
            keyList = kv.getKeyboard().getKeys();
        }
    
        @Override
        public void onStartInputView(EditorInfo info, boolean restarting) {
            super.onStartInputView(info, restarting);
            retrieveKeys();
        }
    
        @Override
        public View onCreateInputView() {
            kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
            kv.setOnKeyboardActionListener(this);
            keyboard = new Keyboard(this, R.xml.qwerty);
            kv.setKeyboard(keyboard);
    
            retrieveKeys();
    
            // Set the onTouchListener to be able to retrieve a MotionEvent
            kv.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // For each key in the key list
                    for (Keyboard.Key k : keyList) {
                        // If the coordinates from the Motion event are inside of the key
                        if (k.isInside((int) event.getX(), (int) event.getY())) {
                            // k is the key pressed
                            Log.d("Debugging",
                                    "Key pressed: X=" + k.x + " - Y=" + k.y);
                            int centreX, centreY;
                            centreX = (k.width/2) + k.x;
                            centreY = (k.width/2) + k.x;
                            // These values are relative to the Keyboard View
                            Log.d("Debugging",
                                    "Centre of the key pressed: X="+centreX+" - Y="+centreY);
                        }
                    }
                    // Return false to avoid consuming the touch event
                    return false;
                }
            });
    
            return kv;
        }
    
        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            InputConnection ic = getCurrentInputConnection();
            switch(primaryCode){
                case Keyboard.KEYCODE_DELETE :
                    ic.deleteSurroundingText(1, 0);
                    break;
                case Keyboard.KEYCODE_SHIFT:
                    caps = !caps;
                    keyboard.setShifted(caps);
                    kv.invalidateAllKeys();
                    break;
                case Keyboard.KEYCODE_DONE:
                    ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                    break;
                default:
                    char code = (char)primaryCode;
                    if(Character.isLetter(code) && caps){
                        code = Character.toUpperCase(code);
                    }
                    ic.commitText(String.valueOf(code),1);
            }
        }
    
        @Override
        public void onPress(int primaryCode) {}
    
        @Override
        public void onRelease(int primaryCode) {}
    
        @Override
        public void onText(CharSequence text) {}
    
        @Override
        public void swipeLeft() {}
    
        @Override
        public void swipeRight() {}
    
        @Override
        public void swipeDown() {}
    
        @Override
        public void swipeUp() {}
    }
    

    注意:在摩托罗拉 Moto G XT1039 Android 5.1 API 22 上测试

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多