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

时间:2011-04-06 09:09:26

标签: android webview alertdialog android-softkeyboard

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

完美的解决方案是,如果用户点击网站的文本字段时出现键盘。但是,让键盘与WebView一起显示也可以。有什么想法吗?

3 个答案:

答案 0 :(得分:23)

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

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

答案 1 :(得分:2)

有更好的解决方案(使用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;
        }
    }
    
  2. 在您将AlertDialog设置为内容视图的布局中使用此LocalWebView而不是原始WebView。

答案 2 :(得分:0)

如果它仍然相关,对我来说问题与沉浸式对话+网络视图有关。

我已经覆盖了对话框 show 方法,通过设置标志来实现身临其境 - 在对话框窗口上没有FOCUS,一旦我删除它,我就失去了沉浸式但现在我的网页对话框弹出uo键盘......