在 WebView 中禁用 SoftKeyboard [英] Disable SoftKeyboard inside a WebView

查看:32
本文介绍了在 WebView 中禁用 SoftKeyboard的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个常见问题,但我无法在此处和 Google 上找到有效的解决方案.

I know this is a common question but I can't find here and on Google, a functioning solution.

我的 android 应用中有一个 WebView,里面有一个 JSF 页面,里面有一个简单的 DatePicker 组件:

I have a WebView in my android app with a JSF page inside with a simple DatePicker component:

<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/>

显示带有日历的弹出窗口.

that show a Popup with calendar.

多亏了这个命令,我才设法使弹出窗口正常工作:

Thanks to this command, I manage to make the popup works:

WebView view=(WebView)findViewById(R.id.statistics);
view.getSettings().setJavaScriptEnabled(true);

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

Unfortunally, when I click on input text or the button to show the calendar's popup, the focus goes to Input Text and Android show its keyboard hiding the calendar.

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

I've tried several solution. The last one was this:

 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);
    }

此代码,按照 Android Studio 的建议,在以下位置引发空指针异常:

This code, as suggested by Android Studio, raise a Null Pointer Exception on:

activity.getCurrentFocus().getWindowToken()

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

Other solutions says to insert a special command in manifest but I want to block the keyboard only in the WebView.

有什么建议吗?

推荐答案

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

Add following code in main(parent) layout in your layout.xml

android:descendantFocusability="blocksDescendants"

希望它会奏效.

并在您的 webview 中设置以下属性

and set following property in your webview

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

这篇关于在 WebView 中禁用 SoftKeyboard的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆