为什么每次重复按键后都会调用android KeyboardView.OnKeyboardActionListener.onRelease()?
根据为什么每次重复按键后都会调用android KeyboardView.OnKeyboardActionListener.onRelease()?,android,android-softkeyboard,Android,Android Softkeyboard,根据KeyboardView.OnKeyboardActionListener.onRelease()SDK文档,“对于重复的键,只调用一次”。但是,如果我在Android软键盘示例中将“a”键的isRepeatable设置为true,并记录onPress()、onKey()和onRelease()方法调用,我会得到预期的重复,但我会观察以下日志中的单个按下/重复/释放序列: I/SoftKeyboard(31467): onPress: 97 I/SoftKeyboard(31467): on
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>
<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\u 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() {
}
};
如果你愿意,你可以扩展KeyboardView,并在其中包含所有这些丑陋的逻辑。我没有做很多Android编程,但是使用onLongPress方法检查onRelease方法中的按键是否仍然被按下不是很简单吗
@Override
public void onRelease(int i) {
if(keyboardView.onLongPress(i){
//do nothing
}
else{
Log.i(TAG, "onRelease: " + i);
}
}
除非我弄错了,否则如果您的手指仍在按键上,该方法应返回true,而当您停止时,该方法应返回false 我猜你的钥匙不是一把重复的钥匙……是的,是的。我澄清了原始帖子。你使用的是stock Android键盘吗?你能发布一段设置键可重复的代码吗?@Taha我更新了问题,加入了一个完整的例子。onLongPress()是一个受保护的回调方法,你不需要显式调用它。啊,是的,在我匆忙寻找解决方案的过程中,我似乎浏览了一下。回答得很好。我因一个体面的工作环境而获得赏金。非常感谢。我做了一个错误报告
@Override
public void onRelease(int i) {
if(keyboardView.onLongPress(i){
//do nothing
}
else{
Log.i(TAG, "onRelease: " + i);
}
}