Android软键盘在webView中无法打开

11

我正在使用AlertDialog中的WebView来对用户进行Twitter授权认证。但是当我点击WebView中的字段时,Android键盘不会弹出。下面是我添加WebView到AlertDialog中的代码。我已经隐式地调用了Android键盘,但它会在AlertDialog后面打开键盘。

这是我的代码:

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

您需要明确设置一个View.OnTouchListener。请参阅我的先前问题:在WebView中点击表单字段不显示软键盘 - Shawn Lauzon
你是对的,我的朋友。但是我正在警告对话框中打开Webview。当我触摸Webview时,Android键盘会打开,但在警告对话框后面,而不是在视图顶部。 - Nirav Bhandari
耶!我解决了这个问题。我添加了一个虚拟编辑文本,并将其可见性设置为隐藏。在将Webview添加到线性布局后,将其添加进去。所以现在一切都正常了。当我点击任何字段时,Android键盘会打开。 - Nirav Bhandari
@NiravBhandari 我想知道,为什么添加一个不可见的视图可以解决这个问题? - Raymond Lukanta
5个回答

26

这是我解决这个问题的方法:

添加一个虚拟编辑文本,并将其可见性设置为GONE,并将其添加到包含WebView的LinearLayout中,在将WebView添加到布局后执行此操作。

示例:

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

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


我已经在您的帖子中添加了一个示例片段,以使其更容易“插入”和使用,但如果您不想要它,请随意将其编辑删除。 - Richard J. Ross III
@RichardJ.RossIII 我知道我来晚了,但我只是想指出你是真正的MVP,Richard。 - Miro Markaravanes
这个解决方案完美地运作。但我想知道背后的科学原理是什么? @RichardJ.RossIII,你知道吗? - Raymond Lukanta

5

我正在使用一个包含WebView的PopupWindow,在使用过程中遇到了同样的问题,但是如果在父级中将focusable设置为true,则问题会消失:

popupWindow.setFocusable(true);

希望这能有所帮助!

0

我曾经遇到过类似的问题,并通过以下方式解决:

  1. 我重写了对话框片段的 onCreateView() 方法,并为我的 Web 视图定义了所有视图配置。

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    
        View view = inflater.inflate(R.layout.webview, container, false);
        // 进行一些配置
        // 其他一些操作
    
        loginpage.loadUrl(url);
        return view;
    }
    
  2. onCreateDialog() 方法中,我只添加了以下内容。

    @Override
    public Dialog onCreateDialog(Bundle savedInstaceState) {
        Dialog dialog = super.onCreateDialog(savedInstaceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }
    
在我的情况下,我想显示一个没有标题的对话框。因此,由于DialogBuilder负责创建对话框视图,我决定重写onCreateView()方法,只需调用super.onCreateDialog()并添加我的窗口配置即可。

-1

我也遇到了这个问题,我通过自定义对话框来解决了这个问题,以下是我的自定义对话框代码,希望对你有用。

    TwitterDialog fb=new TwitterDialog(this);
    fb.abc();
    //fb.dismiss();



class TwitterDialog extends Dialog {
    Context context;
    String url="https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2";
     public TwitterDialog(Context context) {
            super(context);
            this.context=context;
        }
 void abc(){
            LinearLayout mContent = new LinearLayout(context);
            mContent.setOrientation(LinearLayout.VERTICAL);
            final float scale = context.getResources().getDisplayMetrics().density;
            float[] dimensions =new float[]{400.0f,500.0f};

            addContentView(mContent, new FrameLayout.LayoutParams(
                    (int) (dimensions[0] * scale + 0.5f),
                    (int) (dimensions[1] * scale + 0.5f)));

            FrameLayout.LayoutParams FILL =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                 ViewGroup.LayoutParams.FILL_PARENT);
            WebView mWebView = new WebView(context);
            mWebView.setVerticalScrollBarEnabled(false);
            mWebView.setHorizontalScrollBarEnabled(false);
            mWebView.setWebViewClient(new WebClicent());
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.loadUrl(url);
            mWebView.setLayoutParams(FILL);
            mContent.addView(mWebView);

            TwitterDialog.this.show();
        }

         class WebClicent extends WebViewClient{

            @Override
            public void onLoadResource(WebView view, String url) {
                System.out.println("onLoadResource "+url);
                super.onLoadResource(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("onPageFinished "+url);
                                    //TwitterDialog.this.dismiss();
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                System.out.println("onPageStarted "+url);
                super.onPageStarted(view, url, favicon);
            }

         }
  }//TWitterDialog

2
知道解决方案比仅仅粘贴一些源代码更有用。这并没有回答问题。 - Camsoft

-5
你的键盘可能没有显示出来的原因,很可能是因为你正在使用一个已经有硬键盘的设备。任何带有物理键盘的设备都不需要显示软键盘...这就是为什么它没有显示的原因。因为你的设备或模拟器有一个物理硬键盘。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接