简体   繁体   中英

Disable SoftKeyboard inside a WebView

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

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

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

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

activity.getCurrentFocus().getWindowToken()

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

Any suggests?

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

android:descendantFocusability="blocksDescendants"

Hope it will work.

and set following property in your webview

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

I had a similar issue. I was using a text input with a datetimepicker:

HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" required

JQuery: $('#some_id').datetimepicker({format: \\'YYYY-MM-DD\\'});

However this caused issues because the soft keyboard would pop up when you tried to select a date. I then tried to fix it with the below:

HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" onfocus="blur();" required

This worked but it took way too long to actually open the calendar, but at least the soft keyboard wasn't popping up. This is just to show you what I tried at first.

MY SOLUTION: input type="date" placeholder="Birth Date" id="some_id" name="some_id" value="" required

All I had to do was to change the input type from "text" to "date" and remove the jquery line.

Hope this helps someone. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM