简体   繁体   中英

Android soft Keyboard not open in webView`

I m Using WebView in AlertDialog to authenticate user to twitter. but When i click on field in webview ,android keyboard doesnt open. here is my code that shows how i added webview in alert dialog. i implicitly call android keyboard but it open keyboard behind alert dialog.

here is my code.

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

Here is my solution to this problem:

Put a dummy edit text, and set it's visibility to GONE , and add it to a containing LinearLayout, after adding the WebView to the layout.

Example:

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

Once this is done, everything should work properly, and when you select an item in the WebView, the keyboard appears as expected.

I am using a PopupWindow with a WebView inside it and experienced the same problem, but if I set focusable to true in the parent the problem goes away:

popupWindow.setFocusable(true);

Hope this helps!

I had a similar issue and solved it in this way:

  1. I override the onCreateView() method on the dialog fragment and define all view stuff configuration for my web view.

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.webview, container, false); // do some config // some other stuffs loginpage.loadUrl(url); return view; } 
  2. On the onCreateDialog() method i just add these.

     @Override public Dialog onCreateDialog(Bundle savedInstaceState) { Dialog dialog = super.onCreateDialog(savedInstaceState); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); return dialog; } 

In my case i wanted to show a dialog with no title. So due the DialogBuilder take care of creating dialog's view i decided to override the onCreateView() and just call the super.onCreateDialog() and add my window configuration.

I also suffer from this problem, I solved this problem by customizing Dialog, here is my custom dialog code, hope so this is use full for you.

    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

The reason your keyboard may not be showing up, is probably because you are using a device that already has a hard keyboard. ANY DEVICE WITH A PHYSICAL KEYBOARD does not need to show a soft keyboard... That is why it is not showing. Because your device or your emulator has a physical hard keyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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