android - 处理键盘上的 Enter 和搜索键

标签 android keyboard primary-key enter

嗨,如果有人对谷歌键盘有想法,当你在搜索中输入 url 时,它会显示搜索图标,当你输入短信或在 fb 或 EditText 等地方发表评论时,它会显示退格照片以供引用 This one show search button . 所以下面的代码有效我想添加 将 Enter 按钮与这些键一起输入,因为当我在 url 中使用此键盘输入时,我无法按 enter 键显示结果,它在 whatsapp 和其他应用程序中有效,但在浏览器中无效。

this one show backspace

 public void onKey(int primaryCode, int[] keyCodes) {
    this.previousWord = "";
    if (isWordSeparator(primaryCode) && this.mComposing.length() > 0) {
        this.previousWord = this.mComposing.toString();
        commitTyped(getCurrentInputConnection());
    }
    playClick(primaryCode);
    Keyboard current;
    if (primaryCode == -5) {
        handleBackspace();
    } else if (primaryCode == -1) {
        handleShift();
    } else if (primaryCode == -4) {
        handleClose();
    }else if (primaryCode == -2) {
        if (this.kv.getKeyboard() == this.symbols) {
            current = this.keyboard;
        } else {
            current = this.symbols;
        }
        this.kv.setKeyboard(current);
        if (current == this.symbols) {
            current.setShifted(false);
        }
    } else if (primaryCode == -6) {
        if (this.kv.getKeyboard() == this.eng_keyboard) {
            current = this.keyboard;
        } else {
            current = this.eng_keyboard;
        }
        this.kv.setKeyboard(current);
    }else if (primaryCode == -10) {
        if (this.kv.getKeyboard() == this.keyboard) {
            current = this.eng_keyboard;
        } else {
            current = this.keyboard;
        }
        this.kv.setKeyboard(current);
    }else {
        handleCharacter(primaryCode, keyCodes);
    }
}

这些键有效我想在 url 区域中输入时向其添加输入/搜索键

public void swipeDown() {
    handleClose();
}

public void swipeLeft() {
    pickSuggestionManually(1);
}

public void swipeRight() {
    handleBackspace();
}

public void swipeUp() {
}

private void handleClose() {
    requestHideSelf(0);
    this.mComposing = new StringBuilder();
    setSuggestions(null, false, false);
    updateCandidates();
    this.kv.closing();
}

private void handleCharacter(int primaryCode, int[] keyCodes) {
    if (isInputViewShown() && isInputViewShown() && this.kv.isShifted()) {
        primaryCode = Character.toUpperCase(primaryCode);
    }
    if (isAlphabet(primaryCode) && this.mPredictionOn) {
        this.mComposing.append((char) primaryCode);
        getCurrentInputConnection().setComposingText(this.mComposing, 1);
        updateShiftKeyState(getCurrentInputEditorInfo());
        updateCandidates();
        return;
    }
    getCurrentInputConnection().commitText(String.valueOf((char) primaryCode), 1);
}

private void handleShift() {
    if (this.kv != null && this.eng_keyboard == this.kv.getKeyboard()) {
        checkToggleCapsLock();
        KeyboardView keyboardView = this.kv;
        boolean z = this.mCapsLock || !this.kv.isShifted();
        keyboardView.setShifted(z);
    }
}

private void checkToggleCapsLock() {
    long now = System.currentTimeMillis();
    if (this.mLastShiftTime + 800 > now) {
        this.mCapsLock = !this.mCapsLock;
        this.mLastShiftTime = 0;
        return;
    }
    this.mLastShiftTime = now;
}

最佳答案

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});

关于android - 处理键盘上的 Enter 和搜索键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47099929/

相关文章:

android - Android 中的最大音量环

android - 从守护程序 : '...' is a result of a type we don't have a strategy to handle 收到无效响应

c - ncurses 打印出奇怪的键值

c - 从批处理文件中获取击键

postgresql - 删除主键索引

MySQL 复合键完整性

android:允许创建一个 Activity 别名,其 targetActivity 在 aar/sdk 中

java - 如何创建全局类?

linux - 绑定(bind)具有相同 key 代码的 key ,不会丢失第二个 key

c# - 查找 Firebird 数据库中作为主键的所有列名