【问题标题】:Soft Keyboard not displaying on touch in WebView DialogFragment在 WebView DialogFragment 中触摸时未显示软键盘
【发布时间】:2013-05-14 18:56:55
【问题描述】:

编辑:我查看了错误page for this;没有答案工作。看来这是一个尚未解决的Android 系统错误。

首先我提到了this similar question. 但该问题的解决方案似乎不是我的解决方案。我有一个DialogFragment,它只包含一个WebViewWebView 中的所有内容似乎都可以触摸。但是,问题是当我触摸一个表单域时,光标出现了,但软键盘从来没有出现过!

这是我在 DialogFragment 类中的 onCreateDialog() 方法中的代码:

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

如何在选择表单域时显示软键盘?

【问题讨论】:

    标签: java android android-webview touch-event android-dialogfragment


    【解决方案1】:

    这是一个尚未修复的系统错误。 More information can be found here. 似乎这个错误对人们来说发生的方式不同,因此有不同的解决方案。对于我的特殊情况,只有一个解决方案(因为我已经尝试了其他所有方法)。解决方案:

    首先,我为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>
    

    然后,在DialogFragment 类中的onCreateDialog 方法中:

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

    仅此而已。之所以可行,是因为我制作了一个EditText,我将其重点放在了它上面,但却使它不可见。由于EditText 是不可见的,因此它不会干扰WebView,并且由于它具有焦点,因此可以适当地向上拉软键盘。我希望这有助于任何陷入类似情况的人。

    【讨论】:

    • 对我来说,我必须同时做这个 hack 和这个:stackoverflow.com/a/4977247/445348
    • 在 2021 年的 SDK 30 上,这仍然是一个错误,并且仍然是解决方法。
    • 在 2022 年的 SDK 31 上,这仍然是一个错误,并且仍然是解决方法。
    猜你喜欢
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2018-07-13
    • 2023-04-01
    • 2022-11-18
    相关资源
    最近更新 更多