Soft Keyboard not displaying on touch in WebView DialogFragment

試著忘記壹切 提交于 2021-02-19 07:07:24

问题


Edit: I have looked at the error page for this; no answers work. It seems it is an Android system bug that has not yet been solved.

First off I've referred to this similar question. But the solution to that question does not seem to be the solution to mine. I have a DialogFragment that contains only a WebView. Everything in the WebView seems to be touchable. However, the problem is that when I touch a form field, the cursor appears but the soft keyboard never shows up!

Here's my code in the onCreateDialog() method within the DialogFragment class:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    WebView web = new WebView(getActivity());
    web.loadUrl(InternetDialog.this.url);
    web.setFocusable(true);
    web.setFocusableInTouchMode(true);
    web.requestFocus(View.FOCUS_DOWN);
    web.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
            return false;
        }
    });

    builder.setView(web);
    return builder.create();

How can I get the soft keyboard to show up when I select a form field?


回答1:


This is a system bug that has not yet been fixed. More information can be found here. It seems as though this bug occurs differently for people and therefore has different solutions. For my particular case, there is only one solution (as I've tried everything else). Solution:

First, I created a layout for the Dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:visibility="invisible"/>

<WebView
    android:id="@+id/web"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

</RelativeLayout>

Then, in the DialogFragment class in the onCreateDialog method:

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = LayoutInflater.from(getActivity());
    View v = inflater.inflate(R.layout.internet_dialog, null);

    WebView web = (WebView) v.findViewById(R.id.web);
    EditText edit = (EditText) v.findViewById(R.id.edit);
    edit.setFocusable(true);
    edit.requestFocus();
    web.loadUrl(url);
    this.webView = web;
    builder.setView(v);

    return builder.create();

And that's all there was to it. The reason this worked was because I made an EditText which I gave the focus to yet made invisible. Since the EditText is invisible it doesn't interfere with the WebView and since it has focus it pulls the soft keyboard up appropriately. I hope this helps any stuck in a similar situation.



来源:https://stackoverflow.com/questions/16550737/soft-keyboard-not-displaying-on-touch-in-webview-dialogfragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!