简体   繁体   English

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

[英]Show soft keyboard in AlertDialog with a WebView inside (Android)

In my Android app I create an AlertDialog that has a WebView inside. 在我的Android应用程序中,我创建了一个AlertDialog WebViewAlertDialog The WebView loads a webpage that requires the user to log in. However, when I click on text fields in the WebView , soft keyboard does not appear. WebView加载需要用户登录的网页。但是,当我单击WebView文本字段时,不会出现软键盘。 I am aware of the issue in general ( Android: Issue 7189 ); 我一般都知道这个问题( Android:Issue 7189 ); however, in my case the suggested solution does not seem to work since I use an external website, and not just a simple HTML form. 但是,在我的情况下,建议的解决方案似乎不起作用,因为我使用外部网站,而不仅仅是一个简单的HTML表单。

The perfect solution would be if the keyboard appeared when the user clicked on the website's text fields. 如果用户点击网站的文本字段时出现键盘,那么完美的解决方案就是。 However, having the keyboard appear together with the AlertDialog would also work. 但是,让键盘与AlertDialog一起显示也可以。 Any ideas? 有任何想法吗?

It seems that the best solution is to simply create a custom dialog. 似乎最好的解决方案是简单地创建一个自定义对话框。 Custom dialogs do not appear to have the soft keyboard bug at all (it shows up exactly when it has to). 自定义对话框似乎根本没有软键盘错误(它完全显示在必要时)。 Here's some basic code: 这是一些基本代码:

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

There much better solution (with AlertDialog). 有更好的解决方案(使用AlertDialog)。

  1. Extend WebView class. 扩展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. Use this LocalWebView instead of origin WebView in layout that you set to AlertDialog as content view. 在您将AlertDialog设置为内容视图的布局中使用此LocalWebView而不是原始WebView。

If it's still relevant, for me the problem was related to an immersive dialog + webview. 如果它仍然相关,对我来说问题与沉浸式对话+ webview有关。

I've override the dialog show method to achieve immersive by setting a flag - NOT FOCUS on the dialog window, once I removed it I lost the immersivnes but now my web dialog pops uo the keyboard... 我通过在对话框窗口设置一个标志 - NOT FOCUS来覆盖对话框show方法来实现身临其境,一旦我删除了它,我就失去了沉浸感但现在我的网页对话框弹出了键盘......

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

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