在 webView 中阻止键盘

Prevent keyboard in webView

我希望软键盘不会因为我的webView中的一个动作而弹出。那是因为我有一个自定义 "keyboard" 由 webView 下方的按钮组成。但是,我不想为我的应用程序完全禁用键盘,因为我必须在不同的上下文中使用它。当用户单击 webView 中的输入字段时,它不应该显示。我也不希望键盘再次显示并立即隐藏。

目前我的 AndroidManifest.xml 中有这个:

android:windowSoftInputMode="stateAlwaysHidden"

我已经尝试禁用 webView 的焦点,但是我也无法使用我的自定义 "keyboard" 输入文本,因为 webView 的输入字段没有焦点。

我也在 onCreate 中尝试过这个,但它没有用(键盘仍然出现):

View focusedView = this.getCurrentFocus();
if (focusedView == null)
    return;
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager == null)
    return;
manager.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

抱歉我来晚了。

但这是解决方案:

在您的父布局中添加:

android:descendantFocusability="blocksDescendants"

设置 WebView 的这两个属性:

android:focusable="false"
android:focusableInTouchMode="true"

这对我有用:)

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

在一切之前调用它

将此粘贴到您的 onCreate 方法中 setContentView

之后

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

当您 运行 这个 activity 时键盘不会显示。

在你的WebView

中使用它
 android:descendantFocusability="blocksDescendants"
 android:focusable="false"
 android:focusableInTouchMode="false"

试试这个

<WebView
    android:id="@+id/myWebView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false" />