Android软键盘无法在webView`中打开

Nir*_*ari 10 android android-widget android-layout

我在AlertDialog中使用WebView来验证用户到twitter.但是当我点击webview中的字段时,android键盘不会打开.这是我的代码,显示我如何在警报对话框中添加webview.我隐含地调用android键盘,但它在警告对话框后面打开键盘.

这是我的代码.

public static void webViewDialog(final String authorizationURL, final int type)
{

    final boolean correctPin;
    System.out.println("In webViewDialog");
    container = new LinearLayout(context);
    container.setOrientation(LinearLayout.VERTICAL);

    container.setMinimumWidth(200);
    container.setMinimumHeight(320); 

    webView = new WebView(context);
    webView.setMinimumWidth(200);
    webView.requestFocusFromTouch();
    webView.setMinimumHeight(380);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new TwitterWebViewClient());
    webView.loadUrl(authorizationURL);
    webView.setBackgroundColor(0xFFbbd7e9);
    container.addView(webView);

    Builder webDialog = new AlertDialog.Builder(context);

    webDialog.setView(container).setTitle("Twitter Login")
            .setPositiveButton("Ok", new DialogInterface.OnClickListener()
        {

                @Override
                public void onClick(DialogInterface dialog, int which) 
                {

                    if (type == 0) 
                    {
                          twitterPinCodeDialog();
                        dialog.dismiss();

                    }
                }
        }).show();
    showVirtualKeyboard();

}
public static void showVirtualKeyboard()
{
    Timer timer = new Timer();
    timer.schedule(new TimerTask()
    {

         @Override
         public void run()
         {
              InputMethodManager m = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

              if(m != null)
              {
                // m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                m.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
              } 
         }

    }, 100);  
}
Run Code Online (Sandbox Code Playgroud)

Nir*_*ari 21

以下是我对此问题的解决方案:

GONEWebView添加到布局后,放置一个虚拟编辑文本,并将其可见性设置为,并将其添加到包含LinearLayout.

例:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

LinearLayout wrapper = new LinearLayout(this);
WebView webView = new WebView(this);
EditText keyboardHack = new EditText(this);

keyboardHack.setVisibility(View.GONE);

webView.loadUrl(url);

wrapper.setOrientation(LinearLayout.VERTICAL);
wrapper.addView(webView, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
wrapper.addView(keyboardHack, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);              

builder.setView(wrapper);

builder.create().show();
Run Code Online (Sandbox Code Playgroud)

完成后,一切都应该正常工作,当您在WebView中选择一个项目时,键盘会按预期显示.


drg*_*inm 5

我正在使用一个带有 WebView 的 PopupWindow 并遇到了同样的问题,但是如果我在父级中将 focusable 设置为 true 问题就会消失:

popupWindow.setFocusable(true);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!