为什么在每次重复键后调用android KeyboardView.OnKeyboardActionListener.onRelease()?

dav*_*hoo 6 android android-softkeyboard

根据KeyboardView.OnKeyboardActionListener.onRelease()SDK文档,"对于重复的键,这只被调用一次".但是,如果我设置isRepeatable真要与Android Softkeyboard比如"一"键,和日志onPress(),onKey()以及onRelease()方法调用,我得到重复的预期,但我遵守以下日志,只需按一下/复读/释放顺序:

I/SoftKeyboard(31467): onPress: 97
I/SoftKeyboard(31467): onKey: 97
I/SoftKeyboard(31467): onRelease: 97
I/SoftKeyboard(31467): onKey: 97
I/SoftKeyboard(31467): onRelease: 97
I/SoftKeyboard(31467): onKey: 97
I/SoftKeyboard(31467): onRelease: 97
I/SoftKeyboard(31467): onKey: 97
I/SoftKeyboard(31467): onRelease: 97
I/SoftKeyboard(31467): onKey: 97
I/SoftKeyboard(31467): onRelease: 97
Run Code Online (Sandbox Code Playgroud)

如何确定触摸设备的确切释放时间?感谢:D.

编辑(Paul Boddington编辑30/07/2015)

虽然我不是OP,但我想提供一个显示问题的完整示例.

MyActivity:

public class MyActivity extends Activity {

    private static final String TAG = "MyActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_view);
        keyboardView.setKeyboard(new Keyboard(this, R.xml.keyboard));
        keyboardView.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {

            @Override
            public void onPress(int i) {
                Log.i(TAG, "onPress: " + i);
            }

            @Override
            public void onKey(int i, int[] ints) {
                Log.i(TAG, "onKey: " + i);
            }

            @Override
            public void onRelease(int i) {
                Log.i(TAG, "onRelease: " + i);
            }

            @Override public void onText(CharSequence charSequence) {}
            @Override public void swipeLeft() {}
            @Override public void swipeRight() {}
            @Override public void swipeDown() {}
            @Override public void swipeUp() {}
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android">
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    >
    <Row
        android:keyWidth="25%p"
        android:keyHeight="60dp">
        <Key android:codes="0" android:keyLabel="0" android:isRepeatable="true"/>
        <Key android:codes="1" android:keyLabel="1" />
        <Key android:codes="2" android:keyLabel="2" />
        <Key android:codes="3" android:keyLabel="3" />
    </Row>
</Keyboard>
Run Code Online (Sandbox Code Playgroud)

activity_my.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.inputmethodservice.KeyboardView
        android:id="@+id/keyboard_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Tre*_*ers 2

我不知道如何修复KeyboardView以正确发出onRelease()可重复键。因此,我想出了一个解决方法,通过查看MotionEvent.ACTION_UP. 您仅对可重复键执行此操作,并忽略onRelease()这些键的事件。

\n\n
private List<Integer> mRepeatableKeys = new ArrayList<Integer>();\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n\n    final KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_view);\n    keyboardView.post(new Runnable() {\n        @Override\n        public void run() {\n            for( Keyboard.Key key : keyboardView.getKeyboard().getKeys() )\n            {\n                if(key.repeatable) mRepeatableKeys.add(key.codes[0]);\n            }\n        }\n    });\n    keyboardView.setOnTouchListener(new View.OnTouchListener() {\n        @Override\n        public boolean onTouch(View v, MotionEvent event) {\n            if (event.getAction() == MotionEvent.ACTION_UP) {\n                if(mTrackingRepeatableKey != -1)\n                {\n                    Log.i(TAG, "Repeatable key released: " + mTrackingRepeatableKey);\n                    mTrackingRepeatableKey = -1;\n                }\n            }\n            return false;\n        }\n    });\n\n    keyboardView.setKeyboard(new Keyboard(this, R.xml.keyboard));\n    keyboardView.setOnKeyboardActionListener(mKeyboardActionListener);\n}\n\nprivate int mTrackingRepeatableKey = -1;\nKeyboardView.OnKeyboardActionListener mKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {\n\n    @Override\n    public void onPress(int i) {\n        Log.i(TAG, "onPress: " + i);\n        if( mRepeatableKeys.contains(i))\n        {\n            mTrackingRepeatableKey = i;\n        }\n    }\n\n    @Override\n    public void onKey(int i, int[] ints) {\n        if( mRepeatableKeys.contains(i))\n        {\n            Log.i(TAG, "onKey Repeat: " + i);\n            return;\n        }\n        Log.i(TAG, "onKey: " + i);\n    }\n\n    @Override\n    public void onRelease(int i) {\n        // Ignore release for repeatable keys\n        if( mRepeatableKeys.contains(i)) return;\n        Log.i(TAG, "onRelease: " + i);\n    }\n\n    @Override\n    public void onText(CharSequence text) {\n    }\n\n    @Override\n    public void swipeLeft() {\n    }\n\n    @Override\n    public void swipeRight() {\n    }\n\n    @Override\n    public void swipeDown() {\n    }\n\n    @Override\n    public void swipeUp() {\n    }\n};\n
Run Code Online (Sandbox Code Playgroud)\n\n

在安卓5.1上测试。日志现在显示:

\n\n
I/MainActivity\xef\xb9\x95 onPress: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 onKey Repeat: 1\nI/MainActivity\xef\xb9\x95 Repeatable key released: 1\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果您愿意,您可以扩展 KeyboardView 并将所有这些丑陋的逻辑包含在其中。

\n