在AlertDialog中显示带有WebView的软键盘(Android)

ern*_*s7a 16 android webview android-softkeyboard android-alertdialog

在我的Android应用程序中,我创建了AlertDialog一个WebView内部.该WebView负载要求用户登录网页.然而,当我点击在文本字段WebView,软键盘不会出现.我一般都知道这个问题(Android:Issue 7189); 但是,在我的情况下,建议的解决方案似乎不起作用,因为我使用外部网站,而不仅仅是一个简单的HTML表单.

如果用户点击网站的文本字段时出现键盘,那么完美的解决方案就是.但是,让键盘与它一起出现AlertDialog也会起作用.有任何想法吗?

ern*_*s7a 24

似乎最好的解决方案是简单地创建一个自定义对话框.自定义对话框似乎根本没有软键盘错误(它完全显示在必要时).这是一些基本代码:

Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.dialog);
dialog.setTitle("My great title");
dialog.setCancelable(true);

dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);

WebView vw = (WebView) dialog.findViewById(R.id.wv);
Run Code Online (Sandbox Code Playgroud)

  • 请分享您的完整代码.我遇到了同样的问题,但我没有在对话框中加载我的webview.我在一个活动中启动它并按下任何文本字段不显示键盘.帮我 (2认同)

hum*_*oid 5

有更好的解决方案(使用 AlertDialog)。

  1. 扩展WebView类。

    public static class LocalWebView extends WebView {
        public LocalWebView(Context context) {
            super(context);
        }
    
        public LocalWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
       }
    
        public LocalWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
            super(context, attrs, defStyleAttr, privateBrowsing);
        }
    
        @Override
        public boolean onCheckIsTextEditor() {
            return true;
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在您设置为 AlertDialog 作为内容视图的布局中,使用此 LocalWebView 而不是原始 WebView。

  • 谢谢,它有效。您也可以直接使用: `new WebView(this){ @Override public boolean onCheckIsTextEditor() { return true; } } };` (4认同)