Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
575 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...