gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:15:51 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com