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

时间:2015-03-15 10:14:42

标签: android android-softkeyboard

根据KeyboardView.OnKeyboardActionListener.onRelease() SDK文档,“对于重复的密钥,只调用一次”。但是,如果我使用Android软键板示例将'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>

2 个答案:

答案 0 :(得分:2)

我无法弄清楚如何修复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,并在其中包含所有这些丑陋的逻辑。

答案 1 :(得分:0)

我没有做很多Android编程,但是使用onLongPress方法检查是否仍然在onRelease方法中按下了键是不是很简单?

@Override
public void onRelease(int i) {
    if(keyboardView.onLongPress(i){ 
        //do nothing
    }
    else{
        Log.i(TAG, "onRelease: " + i);
    }
}

除非我弄错了,如果你的手指仍然在钥匙上,那么该方法应该返回true,当你停止时,该方法应该返回false。