简体   繁体   中英

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).

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. 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" .
You don't have to use onLongPress .
onKey works as same.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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