简体   繁体   English

软键盘在WebView DialogFragment中不显示触摸

[英]Soft Keyboard not displaying on touch in WebView DialogFragment

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. 它似乎是一个尚未解决的Android系统漏洞。

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 . 我有一个DialogFragment包含WebViewDialogFragment Everything in the WebView seems to be touchable. WebView似乎都是可触摸的。 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: 这是我在DialogFragment类中的onCreateDialog()方法中的DialogFragment

    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? 当我选择表单字段时,如何才能显示软键盘?

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 : 首先,我为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: 然后,在onCreateDialog方法的DialogFragment类中:

    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. 这个工作的原因是因为我制作了一个EditText ,我把焦点放在了隐形的地方。 Since the EditText is invisible it doesn't interfere with the WebView and since it has focus it pulls the soft keyboard up appropriately. 由于EditText是不可见的,因此它不会干扰WebView ,因为它具有焦点,所以它可以适当地拉动软键盘。 I hope this helps any stuck in a similar situation. 我希望这有助于任何陷入类似情况的人。

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

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