WebView textarea doesn't pop up the keyboard

前端 未结 11 1892
野趣味
野趣味 2020-11-28 06:06

When I display a WebView, I don\'t see the soft keyboard popping up. The hard keyboard also doesn\'t work!

What are the usual shortcomings.

The

相关标签:
11条回答
  • 2020-11-28 06:35

    The full solution is a bit more than what Sana had. It is documented as a bug over at the Android site ( http://code.google.com/p/android/issues/detail?id=7189 ):

    webView.requestFocus(View.FOCUS_DOWN);
    webView.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;
        }
    });
    
    0 讨论(0)
  • 2020-11-28 06:36

    The problem was that webview wasn't getting focus when it was loaded hence using

    webView.requestFocus(View.FOCUS_DOWN);
    

    solved the problem.

    0 讨论(0)
  • 2020-11-28 06:36

    Had the same issue and non of the above solutions worked. This woked for me

        <CustomWebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="true"
        android:focusableInTouchMode="true" />
    
    0 讨论(0)
  • 2020-11-28 06:40

    I'm surprised that even on Android 4.1 (tested on SGS3) the problem is still present, and overriding onTouch() don't solve it for me.

    Test code:

    String HTML = "<html><head>TEST</head><body><form>";
    HTML += "<INPUT TYPE=TEXT SIZE=40 NAME=user value=\"your name\">";
    HTML += "</form></body></html>";
    
    WebView wv = new WebView(getContext());
    wv.loadData(HTML, "text/html", null);
    
    final AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
    adb.setTitle("TEST").setView(wv).show();
    

    My complex solution is replace WebView with MyWebView:

    private static class MyWebView extends WebView
    {
        public MyWebView(Context context)
        {
            super(context);
        }
    
        // Note this!
        @Override
        public boolean onCheckIsTextEditor()
        {
            return true;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev)
        {
            switch (ev.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!hasFocus())
                        requestFocus();
                break;
            }
    
            return super.onTouchEvent(ev);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:49

    I had the same problem on Jelly Bean, but none of the above solutions worked for me. This solution worked for me on JellyBean

    WebView webView = new WebView(getActivity(), null, android.R.attr.webViewStyle);
    

    Sometimes the android.R.attr.webViewStyle is not applied by default. This ensures that the style is always applied.

    0 讨论(0)
  • 2020-11-28 06:50

    For those of you looking for a solution when the WebView is in an AlertDialog, the answer here solved this problem for me: Show soft keyboard in AlertDialog with a WebView inside (Android)

    0 讨论(0)
提交回复
热议问题