禁用WebView内的SoftKeyboard [英] Disable SoftKeyboard inside a WebView

查看:86
本文介绍了禁用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的建议,此代码会在以下位置引发Null Pointer异常:

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的主(父)布局中添加以下代码

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

android:descendantFocusability="blocksDescendants"

希望它会起作用.

并在您的网络视图中设置以下属性

and set following property in your webview

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

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

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