Prevent Buttons From Hiding Soft Keyboard On Android

穿精又带淫゛_ 提交于 2019-11-28 12:17:09

The solution to your problem might be to keep the keyboard always shown and letting the user to close it when the actions are done.

InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Try this code in the onCreate() method of your Activity. Note that if the user presses the close button on the keyboard or the back button it should close it. And I guess you shouldn't interfere with that scenario. And when the actions are done you can then close the keyboard from code.

Erhannis

I'm in exactly the same boat, so if you ever found an ideal solution, I'd love to hear it.

EDIT: Success! Or much better than before, anyway. I've deleted my old solution, since this one is better.

As stated in https://stackoverflow.com/a/10536033/513038, it turns out that loadData() hides the keyboard. However, I discovered that in the WebView's hideSoftKeyboard(), it checks the InputMethodManager to see if the webview is active, via imm.isActive(mWebView).

So, if you switch focus to an EditText before loadData(), and switch back to the WebView immediately after, the keyboard sticks around! It briefly switches to upper-case, I think on returning focus to the webview, (actually, this doesn't always seem to happen; it depends) but it's a lot less noticeable than the keyboard flickering away and back.

The gist of what needs to happen is as follows.

Extend WebView. Give it an EditText field:

public EditText mFocusDistraction;

In the constructor, have the following lines:

mFocusDistraction = new EditText(context);
addView(mFocusDistraction);

Then override loadUrl():

public void loadUrl(String s) {
    mFocusDistraction.requestFocus();
    super.loadUrl(s);
    this.requestFocus();
}

That should get it working, basically. It's a bit buggy, though, so here's a more complete class:

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.widget.EditText;

public class WebViewMod extends WebView {
    public EditText mFocusDistraction;
    public Context mContext;

    public WebViewMod(Context context) {
        super(context);
        init(context);
    }    

    public WebViewMod(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }    

    public WebViewMod(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public WebViewMod(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        init(context);
    }

    public void init(Context context) {
        // This lets the layout editor display the view.
        if (isInEditMode()) return;

        mContext = context;

        mFocusDistraction = new EditText(context);
        mFocusDistraction.setBackgroundResource(android.R.color.transparent);
        this.addView(mFocusDistraction);
        mFocusDistraction.getLayoutParams().width = 1;
        mFocusDistraction.getLayoutParams().height = 1;
    }

    @Override
    public void loadUrl(final String url) {
        if (mContext instanceof Activity && this.isFocused()) {
            ((Activity)mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mFocusDistraction.requestFocus();
                    WebViewMod.super.loadUrl(url);
                    WebViewMod.this.requestFocus();
                }
            });
        } else {
            super.loadUrl(url);
        }
    }
}

Xamarin Android (Alternative):

InputMethodManager inputManager = (InputMethodManager)GetSystemService(Context.InputMethodService); 
inputManager.ToggleSoftInput (ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!