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

标签 android android-softkeyboard

根据 KeyboardView.OnKeyboardActionListener.onRelease() SDK 文档,“对于重复的键,只调用一次”。但是,如果我使用 Android Softkeyboard 示例将“a”键的 isRepeatable 设置为 true,并记录 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

如何准确确定触摸设备何时被释放?谢谢,D。

编辑(Paul Boddington 编辑,2015 年 7 月 30 日)

虽然我不是 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() {}
        });
    }
}

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>

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>

最佳答案

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

private List<Integer> mRepeatableKeys = new ArrayList<Integer>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final KeyboardView keyboardView = (KeyboardView) findViewById(R.id.keyboard_view);
    keyboardView.post(new Runnable() {
        @Override
        public void run() {
            for( Keyboard.Key key : keyboardView.getKeyboard().getKeys() )
            {
                if(key.repeatable) mRepeatableKeys.add(key.codes[0]);
            }
        }
    });
    keyboardView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if(mTrackingRepeatableKey != -1)
                {
                    Log.i(TAG, "Repeatable key released: " + mTrackingRepeatableKey);
                    mTrackingRepeatableKey = -1;
                }
            }
            return false;
        }
    });

    keyboardView.setKeyboard(new Keyboard(this, R.xml.keyboard));
    keyboardView.setOnKeyboardActionListener(mKeyboardActionListener);
}

private int mTrackingRepeatableKey = -1;
KeyboardView.OnKeyboardActionListener mKeyboardActionListener = new KeyboardView.OnKeyboardActionListener() {

    @Override
    public void onPress(int i) {
        Log.i(TAG, "onPress: " + i);
        if( mRepeatableKeys.contains(i))
        {
            mTrackingRepeatableKey = i;
        }
    }

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

    @Override
    public void onRelease(int i) {
        // Ignore release for repeatable keys
        if( mRepeatableKeys.contains(i)) return;
        Log.i(TAG, "onRelease: " + i);
    }

    @Override
    public void onText(CharSequence text) {
    }

    @Override
    public void swipeLeft() {
    }

    @Override
    public void swipeRight() {
    }

    @Override
    public void swipeDown() {
    }

    @Override
    public void swipeUp() {
    }
};

在 Android 5.1 上测试。日志现在显示为:

I/MainActivity﹕ onPress: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ onKey Repeat: 1
I/MainActivity﹕ Repeatable key released: 1

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

关于android - 为什么在每次重复按键后调用 android KeyboardView.OnKeyboardActionListener.onRelease()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29059489/

相关文章:

android - 当任一编辑文本获得焦点时,软键盘顶部的黑色矩形

android - 第一次加载 webview 时,软键盘会自动弹出

android - 让 Room DAO 返回 LiveData<Cursor>

android - 动态链接到 Android 中的共享 Java 库

android - 是否有与 RecyclerView 等效的 addHeaderView?

安卓 IMU 精度

android - 单击按钮更改 TextView 文本 (Android)

android - 在 TimePicker 中使用键盘输入需要在 "Enter"之前按 "OK",否则值将被丢弃

android - 如何向 android ime 添加文本预测支持?

android - 如何允许android中软键盘后面的内容?