开发者

Tapping form field in WebView does not show soft keyboard

I created my开发者_如何转开发 own WebView and set the WebChromeClient and WebViewClient objects. When I start this WebView, the HTML form fields react when I touch them (a cursor appears), but they do not get selected, nor does the soft keyboard start. If I use the trackball to choose the form and press it, the keyboard does appear.

I tried to call myWebview.requestFocusFromTouch() as this answer suggested, but it returns false and doesn't help.


http://code.google.com/p/android/issues/detail?id=7189

Here is a fix in case other were not clear.

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


Agreed 10% with Dv_MH's answer. However, the attached class was a little excessive. All I needed to do was extend WebView & return true for onCheckIsTextEditor()

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;

public class LiveWebView extends WebView {

    public LiveWebView(Context context) {
        super(context);
    }

    public LiveWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LiveWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onCheckIsTextEditor() {
        return true;
    }
}

From there, I was able to use it as a normal WebView (even in Dialogs).

Alternatively, with less code:

WebView web = new WebView(context) {
  @Override
  public boolean onCheckIsTextEditor() {
    return true;
  }
};


IMPORTANT I spent hours searching for this and I solved it by making sure that parent layout that extends "ViewGroup" doesn't have property

android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused

OR Add android:focusable="true" to webview


AndroidChen's answer did not work for me at all, but I searched the link provided (http://code.google.com/p/android/issues/detail?id=7189) and I found the following class, it works perfectly (Android Froyo on HTC Bravo), not just text, but also all buttons, redirects, etc, everything works perfectly :D

class LiveWebView extends WebView
{
    Context mContext;

    public LiveWebView(Context context, String URL)
    {
        super(context);
        mContext = context;
        setWebViewClient(URL);
    }

    @Override
    public boolean onCheckIsTextEditor()
    {
        return true;
    }

    @SuppressLint("SetJavaScriptEnabled")
    boolean setWebViewClient(String URL)
    {
        setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY);
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus(View.FOCUS_DOWN);

        WebSettings webSettings = getSettings();
        webSettings.setSavePassword(false);
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(false);
        webSettings.setUseWideViewPort(false);

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

        this.setWebViewClient(new WebViewClient()
        {
            ProgressDialog dialog = new ProgressDialog(mContext);

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                loadUrl(url);

                return true;
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }

            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                if (dialog != null)
                {
                    dialog.setMessage("Loading...");
                    dialog.setIndeterminate(true);
                    dialog.setCancelable(true);
                    dialog.show();
                }

            }

            public void onPageFinished(WebView view, String url)
            {
                if (dialog != null)
                {
                    dialog.cancel();
                }
            }
        });

        this.setWebChromeClient(new WebChromeClient()
        {
            public void onProgressChanged(WebView view, int progress)
            {
                // Activities and WebViews measure progress with different scales.
                // The progress meter will automatically disappear when we reach 100%
            }

            @Override
            public boolean onJsAlert(WebView view, String url, String message, JsResult result)
            {
                result.confirm();
                return true;
            }
        });

        loadUrl(URL);

        return true;
    }
}

and then to create a webview within a dialog, I wrote this code

AlertDialog.Builder alert = new AlertDialog.Builder(M_PaymentOptions.this);
alert.setNegativeButton("Back to Payment Options", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int id)
    {}
});

alert.setTitle("BarclayCard Payment");

LiveWebView liveWevViewObject = new LiveWebView(M_PaymentOptions.this, redirecturl);

alert.setView(liveWevViewObject);

alert.show();


Just add invisible EditText under WebView

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="invisible" />

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

All these focus-related solutions didn't work on my Samsung Note 3/Android 5.0


As mentioned here on Google Issue Tracker this fix works fine. Nothing else needed.

webView.isFocusable = true
webView.isFocusableInTouchMode = true
webView.requestFocus(View.FOCUS_DOWN)


For anyone who is having this in a DialogFragment (or maybe a Dialog in general), this may be related to this bug: https://issuetracker.google.com/issues/36915710

Here's the workaround which worked for me:

https://stackoverflow.com/a/16573061/1316677

Summary: create an invisible/"gone" EditText in the layout in the Dialog, then call:

     EditText edit = view.findViewById(R.id.editText);
     edit.setFocusable(true);
     edit.requestFocus();


In my case (Set top / Media Player type device with Android 7.1.1) none of these solutions were needed.

It turns out that having a physical keyboard plugged in prevents the soft keyboard from showing by default. Make sure you unplug any physical keyboards (or anything that presents itself as a keyboard) from the device before attempting to test your webivew. By default soft keyboard will not show if there is a physical keyboard detected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜