在WebView中禁用SoftKeyboard

MrM*_*ime 5 android webview

我知道这是一个常见的问题,但我无法在谷歌找到这个有效的解决方案.

我的Android应用程序中有一个WebView,其中包含一个简单的DatePicker组件的JSF页面:

<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/>
Run Code Online (Sandbox Code Playgroud)

显示带日历的Popup.

感谢这个命令,我设法让弹出窗口工作:

WebView view=(WebView)findViewById(R.id.statistics);
view.getSettings().setJavaScriptEnabled(true);
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我点击输入文本或按钮显示日历的弹出窗口时,焦点转到输入文本,Android显示其键盘隐藏日历.

我试过几个解决方案.最后一个是这样的:

 view.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            hideSoftKeyboard(v);
            return false;
        }
    });

public void hideSoftKeyboard(View v) {
        Activity activity = (Activity) v.getContext();
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
Run Code Online (Sandbox Code Playgroud)

Android Studio提供的此代码会在以下情况下引发Null Pointer Exception:

activity.getCurrentFocus().getWindowToken()
Run Code Online (Sandbox Code Playgroud)

其他解决方案说在清单中插入一个特殊命令,但我想只在WebView中阻止键盘.

有什么建议?

And*_*eam 11

在layout.xml中的main(父)布局中添加以下代码

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)

希望它会奏效.

并在webview中设置以下属性

android:focusable="false" 
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

4960 次

最近记录:

6 年,3 月 前