Can Android WebView HTML influence keyboard or keyboard features?

前端 未结 3 1786
粉色の甜心
粉色の甜心 2021-02-07 12:03

I have a webview with a credit card entry form (with standard fields). In different versions of Android I get different keyboards. Kit

3条回答
  •  长情又很酷
    2021-02-07 12:26

    You can control the keyboard from the WebView, but as other answers suggest it may not work with every keyboard ever made. Despite that, I typically find most mainstream keyboards have implemented the behaviour I want.

    The WebView has a method called onCreateInputConnection. You can hook into this method and add (and/or remove) flags to the inputType and/or imeOptions. There are many flags available to you.

    Check out the EditorInfo options, in particular IME_FLAG_NAVIGATE_NEXT and IME_FLAG_NAVIGATE_PREVIOUS. See usage below (which removes the prev/next flags from the keyboard options):

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
        outAttrs.imeOptions = outAttrs.imeOptions & ~EditorInfo.IME_FLAG_NAVIGATE_NEXT &
                    ~EditorInfo.IME_FLAG_NAVIGATE_PREVIOUS; 
        return inputConnection;
    }
    

    Another thing you could try is to hide the entire suggestions bar using the InputType flags TYPE_TEXT_FLAG_NO_SUGGESTIONS. See example below (which adds the "no suggestions" flag to the input type):

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        InputConnection inputConnection = super.onCreateInputConnection(outAttrs);
        outAttrs.inputType = outAttrs.inputType | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
        return inputConnection;
    }
    

    There are many other flags available to have a play with to customise the IME directly from the WebView. Refer the developer to the linked pages and you should hopefully be able to achieve the behaviour you are after on most keyboards.

提交回复
热议问题