根据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/