簡體   English   中英

Android軟鍵盤無法在webView`中打開

[英]Android soft Keyboard not open in webView`

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

以下是我對此問題的解決方案:

放置一個虛擬編輯文本,並將其可見性設置為GONE ,並在將WebView添加到布局后將其添加到包含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();

完成后,一切都應該正常工作,當您在WebView中選擇一個項目時,鍵盤會按預期顯示。

我在其中使用帶有WebView的PopupWindow並遇到了同樣的問題,但如果我在父級中將focusable設置為true,則問題就會消失:

popupWindow.setFocusable(true);

希望這可以幫助!

我有一個類似的問題,並以這種方式解決它:

  1. 我重寫對話框片段上的onCreateView()方法,並為我的Web視圖定義所有視圖填充配置。

     @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. onCreateDialog()方法上我只是添加這些。

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

在我的情況下,我想顯示一個沒有標題的對話框。 因此,由於DialogBu​​ilder負責創建對話框的視圖,我決定覆蓋onCreateView()並調用super.onCreateDialog()並添加我的窗口配置。

我也遇到這個問題,我通過自定義Dialog解決了這個問題,這里是我的自定義對話框代碼,希望這對你來說是完整的。

    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

您的鍵盤可能無法顯示的原因可能是因為您使用的是已有硬鍵盤的設備。 任何帶有物理鍵盤的設備都不需要顯示軟鍵盤......這就是它沒有顯示的原因。 因為您的設備或模擬器具有物理硬鍵盤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM