处理键盘上的输入和搜索键

时间:2017-11-03 16:03:06

标签: android keyboard primary-key enter

您好我想如果有任何人有关于Google键盘的想法,当您在搜索中输入网址时会显示搜索图标,当您输入短信或评论fb或EditText等地方时,会显示Backspace照片以供参考This one show search button 。 所以下面的代码正在工作,我想添加 与这些键一起输入按钮,当我在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;
}

2 个答案:

答案 0 :(得分: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;
    }
});

答案 1 :(得分:1)

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