简体   繁体   English

软键盘长按退格键如何工作?

[英]How the soft keyboard long press backspace works?

I'm creating a input method on Android, and I want to implement the long press backspace key to delete character one by one without release (I think the default behavior is press the backspace once got one character deleted). 我正在Android上创建一种输入法,并且我想实现长按退格键来不删除而逐个删除字符(我认为默认行为是删除一个字符后按退格键)。

My current solution is "override" the "onLongPress" function in my sub-class of "KeyboardView", and call a keep running delete to send delete action to the view when the long press with code "KEYCODE_DELETE" was triggered. 我当前的解决方案是“覆盖”我的“ KeyboardView”子类中的“ onLongPress”函数,并在触发长按“ KEYCODE_DELETE”代码的调用时,调用持续运行删除以将删除操作发送到视图。 As my codes below: 如以下代码所示:

    @Override
protected boolean onLongPress(Key key) {
    if (key.codes[0] == Keyboard.KEYCODE_DELETE) {
        final Handler h = new Handler();
        final int delay = 500;
        h.postDelayed(new Runnable() {
            public void run() {
                h.postDelayed(this, delay);
                getOnKeyboardActionListener().onKey(Keyboard.KEYCODE_DELETE, null);
            }
        }, delay);
        return true;
    } else {
        return super.onLongPress(key);
    }
}

So, my question is "how to stop it?" 因此,我的问题是“如何阻止它?” how to track the release of the long press on backspace key? 如何跟踪长按退格键的释放? Or is there any way to make the long press deleting happen? 还是有什么办法可以使长按删除发生?

I'm in the process of studying.n 我正在学习中。
It seems work but I'm not sure it's correct. 看来可行,但我不确定是否正确。

Use android:isRepeatable="true" . 使用android:isRepeatable="true"
You don't have to use onLongPress . 您不必使用onLongPress
onKey works as same. onKey的工作原理相同。

xml XML文件

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="http://schemas.android.com/apk/res/android"
          android:horizontalGap="0px" android:verticalGap="0px"
          android:keyWidth="100%p"    android:keyHeight="70dp">
    <Row>
        <Key android:codes="-5" android:keyIcon="@drawable/ic_action_name" android:isRepeatable="true">
    </Row>
</Keyboard>

java class Java类

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    InputConnection ic = getCurrentInputConnection();

    switch(primaryCode) {
        case Keyboard.KEYCODE_DELETE:
            ic.deleteSurroundingText(1, 0);
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM