I have a WebView and some buttons in my layout. There is a large tag in my WebView. This app is used to edit text files. The buttons are used to effect the textarea inside my WebView. When the user presses a button (Such as an Arrow Button to Move the text view) it closes the keyboard. I have used toggleSoftInput, but that just toggles the keyboard to show or not. I want the buttons to stop hiding the soft keyboard when the button is pressed. I have found nothing about my specific problem. I have searched for weeks. Anybody have any idea on how I can stop my Buttons from hiding the Soft Keyboard on Android?
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.
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);
来源:https://stackoverflow.com/questions/8016430/prevent-buttons-from-hiding-soft-keyboard-on-android