简体   繁体   English

WebView点击表单域不显示软键盘

[英]Tapping form field in WebView does not show soft keyboard

I created my own WebView and set the WebChromeClient and WebViewClient objects.我创建了自己的 WebView 并设置了 WebChromeClient 和 WebViewClient 对象。 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.当我启动这个 WebView 时,HTML 表单域会在我触摸它们时做出反应(出现 cursor),但它们没有被选中,软键盘也没有启动。 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.我尝试按照此答案的建议调用myWebview.requestFocusFromTouch() ,但它返回 false 并且无济于事。

http://code.google.com/p/android/issues/detail?id=7189 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.同意 Dv_MH 的回答 10%。 However, the attached class was a little excessive.然而,附加的课程有点过分。 All I needed to do was extend WebView & return true for onCheckIsTextEditor()我需要做的就是扩展 WebView 并为 onCheckIsTextEditor() 返回 true

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).从那里,我能够将它用作普通的 WebView(甚至在 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重要我花了几个小时寻找这个,我通过确保扩展“ViewGroup”的父布局没有属性来解决它

android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused android:descendantFocusability = "blocksDescendants" 防止子布局获得焦点

OR Add android:focusable="true" to webview添加 android:focusable="true" 到 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 AndroidChen 的答案根本不适合我,但我搜索了提供的链接( http://code.google.com/p/android/issues/detail?id=7189 ),我找到了以下课程,它完美地工作( HTC Bravo 上的 Android Froyo),不仅是文本,还有所有按钮、重定向等,一切正常: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然后在对话框中创建一个 webview,我写了这段代码

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只需在 WebView 下添加不可见的 EditText

<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所有这些与焦点相关的解决方案都不适用于我的三星 Note 3/Android 5.0

As mentioned here on Google Issue Tracker this fix works fine.正如此处Google 问题跟踪器上所述,此修复程序运行良好。 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对于在 DialogFragment(或一般的 Dialog)中拥有此功能的任何人,这可能与此错误有关: https ://issuetracker.google.com/issues/36915710

Here's the workaround which worked for me:这是对我有用的解决方法:

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

Summary: create an invisible/"gone" EditText in the layout in the Dialog, then call:总结:在Dialog的布局中创建一个不可见/“消失”的EditText,然后调用:

     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.在我的例子中(机顶盒/媒体播放器类型设备 Android 7.1.1)不需要这些解决方案。

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.在尝试测试您的 webivew 之前,请确保从设备上拔下任何物理键盘(或任何显示为键盘的东西)。 By default soft keyboard will not show if there is a physical keyboard detected.默认情况下,如果检测到物理键盘,则不会显示软键盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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