【问题标题】:Handling Enter and search key on the keyboard处理键盘上的 Enter 和搜索键
【发布时间】:2018-04-16 10:20:46
【问题描述】:

您好,如果有人对谷歌键盘有想法,当您在搜索中输入 url 时,它会显示搜索图标,当您输入 SMS 或在 fb 或 EditText 等地方发表评论时,它会显示 Backspace 照片以供参考。 所以下面的代码正在工作我想添加 将 Enter 按钮与这些键一起输入,因为当我在 url 中使用此键盘输入时,我无法按 Enter 显示结果,它在 whatsapp 和其他应用程序中有效,但在浏览器中无效。

 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;
}

【问题讨论】:

    标签: android keyboard primary-key enter


    【解决方案1】:
    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;
        }
    });
    

    【讨论】:

    • 在调用键盘的TextEdit上
    • 我没有 TextEdit ,为什么我会有这是关键字应用程序
    【解决方案2】:

    我必须创建两个不同的键盘 xml,一个带有输入按钮,另一个带有退格键。我必须输入带有退格按钮的edittext键盘,然后在搜索栏中输入时输入带有回车按钮的键盘。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 2011-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多