Android:如何在软输入/键盘上捕获长按事件?

时间:2022-12-13 21:44:24

Short version of question: how can I capture long press event on soft input/keyboard in Android?

问题的简短版本:如何在Android中的软输入/键盘上捕获长按事件?

Long version: In an Android app, we have a multi-line EditText, and we want to have this behavior: 1. By default, it's showing a DONE button, by tapping it, soft input/keyboard will be closed. 2. If user long press the DONE button, its behavior will be changed to ENTER button, and there will have a new line in the EditText.

长版:在Android应用程序中,我们有一个多行EditText,我们希望有这样的行为:1。默认情况下,它显示一个DONE按钮,通过点击它,软输入/键盘将被关闭。 2.如果用户长按DONE按钮,其行为将更改为ENTER按钮,EditText中将有一个新行。

For requirement #1, I used the solution in here: https://*.com/a/12570003/4225326

对于需求#1,我在这里使用了解决方案:https://*.com/a/12570003/4225326

For requirement #2, the blocking question I have is, how to capture the long press event. I set the onEditorActionListener, but the captured event is null: http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html I searched the document, the long press related method is for hard keyboard: http://developer.android.com/reference/android/view/View.html#onKeyLongPress(int, android.view.KeyEvent), I can't find one for soft input/keyboard.

对于需求#2,我遇到的阻塞问题是,如何捕获长按事件。我设置了onEditorActionListener,但捕获的事件为null:http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html我搜索了文档,长按相关的方法是硬键盘:http:http: //developer.android.com/reference/android/view/View.html#onKeyLongPress(int,android.view.KeyEvent),我找不到一个用于软输入/键盘。

Thanks for looking into this question.

感谢您查看此问题。

2 个解决方案

#1


0  

I could not find this answer myself, so I manually coded the solution. I used a timer on the onPress() and onRelease() events of the KeyboardView.OnKeyboardActionListener. Here's the important code. Many TRY/CATCHes left out for brevity. In English, when a key is pressed, I'm starting a timer that waits the same time as a long-click event normally waits (ViewConfiguration.getLongPressTimeout()), then executes an on-long-click event on the original thread. Subsequent key releases and key presses can cancel any active timer.

我自己找不到这个答案,所以我手动编写了解决方案。我在KeyboardView.OnKeyboardActionListener的onPress()和onRelease()事件上使用了一个计时器。这是重要的代码。许多TRY / CATCH为了简洁而遗漏了。在英语中,当按下某个键时,我正在启动一个计时器,该计时器等待通常等待的长按事件(ViewConfiguration.getLongPressTimeout()),然后在原始线程上执行on-long-click事件。随后的按键释放和按键可以取消任何活动计时器。

public class MyIME
    extends InputMethodService
    implements KeyboardView.OnKeyboardActionListener {
    :
    :
    private Timer timerLongPress  = null;
    :
    :

    @Override
    public void onCreate() {
        super.onCreate();
        :
        :
        timerLongPress = new Timer();
        :
        :
    }

    @Override
    public void onRelease(final int primaryCode) {
        :
        :
        timerLongPress.cancel();
        :
        :
    }

    @Override
    public void onPress(final int primaryCode) {
        :
        :
        timerLongPress.cancel();
        :
        :
        timerLongPress = new Timer();

        timerLongPress.schedule(new TimerTask() {

            @Override
            public void run() {

                try {

                    Handler uiHandler = new Handler(Looper.getMainLooper());

                    Runnable runnable = new Runnable() {

                        @Override
                        public void run() {

                            try {

                                MyIME.this.onKeyLongPress(primaryCode);

                            } catch (Exception e) {
                                Log.e(MyIME.class.getSimpleName(), "uiHandler.run: " + e.getMessage(), e);
                            }

                        }
                    };

                    uiHandler.post(runnable);

                } catch (Exception e) {
                    Log.e(MyIME.class.getSimpleName(), "Timer.run: " + e.getMessage(), e);
                }
            }

        }, ViewConfiguration.getLongPressTimeout());
        :
        :
    }

    public void onKeyLongPress(int keyCode) {
        // Process long-click here
    }

#2


0  

Create a custom Keyboard View class by extending the KeyboardView class. Override the onLongPress() method

通过扩展KeyboardView类创建自定义键盘视图类。覆盖onLongPress()方法

#1


0  

I could not find this answer myself, so I manually coded the solution. I used a timer on the onPress() and onRelease() events of the KeyboardView.OnKeyboardActionListener. Here's the important code. Many TRY/CATCHes left out for brevity. In English, when a key is pressed, I'm starting a timer that waits the same time as a long-click event normally waits (ViewConfiguration.getLongPressTimeout()), then executes an on-long-click event on the original thread. Subsequent key releases and key presses can cancel any active timer.

我自己找不到这个答案,所以我手动编写了解决方案。我在KeyboardView.OnKeyboardActionListener的onPress()和onRelease()事件上使用了一个计时器。这是重要的代码。许多TRY / CATCH为了简洁而遗漏了。在英语中,当按下某个键时,我正在启动一个计时器,该计时器等待通常等待的长按事件(ViewConfiguration.getLongPressTimeout()),然后在原始线程上执行on-long-click事件。随后的按键释放和按键可以取消任何活动计时器。

public class MyIME
    extends InputMethodService
    implements KeyboardView.OnKeyboardActionListener {
    :
    :
    private Timer timerLongPress  = null;
    :
    :

    @Override
    public void onCreate() {
        super.onCreate();
        :
        :
        timerLongPress = new Timer();
        :
        :
    }

    @Override
    public void onRelease(final int primaryCode) {
        :
        :
        timerLongPress.cancel();
        :
        :
    }

    @Override
    public void onPress(final int primaryCode) {
        :
        :
        timerLongPress.cancel();
        :
        :
        timerLongPress = new Timer();

        timerLongPress.schedule(new TimerTask() {

            @Override
            public void run() {

                try {

                    Handler uiHandler = new Handler(Looper.getMainLooper());

                    Runnable runnable = new Runnable() {

                        @Override
                        public void run() {

                            try {

                                MyIME.this.onKeyLongPress(primaryCode);

                            } catch (Exception e) {
                                Log.e(MyIME.class.getSimpleName(), "uiHandler.run: " + e.getMessage(), e);
                            }

                        }
                    };

                    uiHandler.post(runnable);

                } catch (Exception e) {
                    Log.e(MyIME.class.getSimpleName(), "Timer.run: " + e.getMessage(), e);
                }
            }

        }, ViewConfiguration.getLongPressTimeout());
        :
        :
    }

    public void onKeyLongPress(int keyCode) {
        // Process long-click here
    }

#2


0  

Create a custom Keyboard View class by extending the KeyboardView class. Override the onLongPress() method

通过扩展KeyboardView类创建自定义键盘视图类。覆盖onLongPress()方法