如何处理 ImeOptions 的完成按钮点击?

2023-02-16移动开发问题
8

本文介绍了如何处理 ImeOptions 的完成按钮点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个 EditText 我正在设置以下属性,以便当用户单击 EditText 时我可以在键盘上显示完成按钮.

I am having an EditText where I am setting the following property so that I can display the done button on the keyboard when user click on the EditText.

editText.setImeOptions(EditorInfo.IME_ACTION_DONE);

当用户单击屏幕键盘上的完成按钮(完成输入)时,我想更改 RadioButton 状态.

When user clicks the done button on the screen keyboard (finished typing) I want to change a RadioButton state.

如何在屏幕键盘点击完成按钮时跟踪它?

How can I track done button when it is hit from screen keyboard?

推荐答案

我最终得到了 Roberts 和 chirags 的组合答案:

I ended up with a combination of Roberts and chirags answers:

((EditText)findViewById(R.id.search_field)).setOnEditorActionListener(
        new EditText.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // Identifier of the action. This will be either the identifier you supplied,
        // or EditorInfo.IME_NULL if being called due to the enter key being pressed.
        if (actionId == EditorInfo.IME_ACTION_SEARCH
                || actionId == EditorInfo.IME_ACTION_DONE
                || event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            onSearchAction(v);
            return true;
        }
        // Return true if you have consumed the action, else false.
        return false;
    }
});

更新:上面的代码有时会激活回调两次.相反,我选择了从 Google 聊天客户端获得的以下代码:

Update: The above code would some times activate the callback twice. Instead I've opted for the following code, which I got from the Google chat clients:

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    // If triggered by an enter key, this is the event; otherwise, this is null.
    if (event != null) {
        // if shift key is down, then we want to insert the '
' char in the TextView;
        // otherwise, the default action is to send the message.
        if (!event.isShiftPressed()) {
            if (isPreparedForSending()) {
                confirmSendMessageIfNeeded();
            }
            return true;
        }
        return false;
    }

    if (isPreparedForSending()) {
        confirmSendMessageIfNeeded();
    }
    return true;
}

这篇关于如何处理 ImeOptions 的完成按钮点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Cocos2d-iPhone 或 Sparrow 第一次 2D iOS 游戏开发?
Cocos2d-iPhone or Sparrow for first time 2D iOS game development?(Cocos2d-iPhone 或 Sparrow 第一次 2D iOS 游戏开发?)...
2024-08-12 移动开发问题
4

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

如何在 cocos2d ios 中播放 BGM 并知道当前播放时间?
How to play a BGM and know current playback time on in cocos2d ios?(如何在 cocos2d ios 中播放 BGM 并知道当前播放时间?)...
2024-08-12 移动开发问题
4

cocos2d 从未来的特定时间开始粒子
cocos2d starting particles from a specific time in the future(cocos2d 从未来的特定时间开始粒子)...
2024-08-12 移动开发问题
5

了解 applicationSignificantTimeChange:
Understanding applicationSignificantTimeChange:(了解 applicationSignificantTimeChange:)...
2024-08-12 移动开发问题
10

iOS 和 Cocos2d - 更改 CCSprite 的图像和新尺寸 = FAIL
iOS and Cocos2d - Changing CCSprite#39;s image AND new dimensions = FAIL(iOS 和 Cocos2d - 更改 CCSprite 的图像和新尺寸 = FAIL)...
2024-08-12 移动开发问题
8