在WebView中点击表单字段不显示软键盘

我创build了自己的WebView并设置了WebChromeClient和WebViewClient对象。 当我启动这个WebView时,当我触摸它们(光标出现)时,HTML表单字段会作出反应,但是它们不会被选中,软键盘也不会启动。 如果我使用轨迹球来select窗体并按下它,键盘确实会出现。

我试图调用myWebview.requestFocusFromTouch()作为这个答案build议,但它返回false,并没有帮助。

http://code.google.com/p/android/issues/detail?id=7189

如果其他人不清楚,这是一个修复。

 webview.requestFocus(View.FOCUS_DOWN); webview.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); 

同意Dv_MH的答案10%。 然而,所附class级有点过分。 我所需要做的就是扩展WebView并返回true onCheckIsTextEditor()

 import android.content.Context; import android.util.AttributeSet; import android.webkit.WebView; public class LiveWebView extends WebView { public LiveWebView(Context context) { super(context); } public LiveWebView(Context context, AttributeSet attrs) { super(context, attrs); } public LiveWebView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onCheckIsTextEditor() { return true; } } 

从那里,我能够使用它作为一个正常的WebView(甚至在对话框中)。

或者,用较less的代码:

 WebView web = new WebView(context) { @Override public boolean onCheckIsTextEditor() { return true; } }; 

重要我花了几个小时寻找这个,我通过确保扩展“ViewGroup”的父布局没有属性

android:descendantFocusability =“blocksDescendants”防止子布局变得焦点

或者将android:focusable =“true”添加到webview

AndroidChen的答案根本不适用于我,但是我search了提供的链接( http://code.google.com/p/android/issues/detail?id=7189 ),并且我find了下面的类,它完美的工作HTC Bravo上的Android Froyo),不仅仅是文本,还包括所有的button,redirect等等,一切都很完美:D

 class LiveWebView extends WebView { Context mContext; public LiveWebView(Context context, String URL) { super(context); mContext = context; setWebViewClient(URL); } @Override public boolean onCheckIsTextEditor() { return true; } @SuppressLint("SetJavaScriptEnabled") boolean setWebViewClient(String URL) { setScrollBarStyle(SCROLLBARS_INSIDE_OVERLAY); setFocusable(true); setFocusableInTouchMode(true); requestFocus(View.FOCUS_DOWN); WebSettings webSettings = getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(false); webSettings.setUseWideViewPort(false); setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!v.hasFocus()) { v.requestFocus(); } break; } return false; } }); this.setWebViewClient(new WebViewClient() { ProgressDialog dialog = new ProgressDialog(mContext); @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { loadUrl(url); return true; } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } public void onPageStarted(WebView view, String url, Bitmap favicon) { if (dialog != null) { dialog.setMessage("Loading..."); dialog.setIndeterminate(true); dialog.setCancelable(true); dialog.show(); } } public void onPageFinished(WebView view, String url) { if (dialog != null) { dialog.cancel(); } } }); this.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { // Activities and WebViews measure progress with different scales. // The progress meter will automatically disappear when we reach 100% } @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { result.confirm(); return true; } }); loadUrl(URL); return true; } } 

然后在对话框中创build一个webview,我写了这个代码

 AlertDialog.Builder alert = new AlertDialog.Builder(M_PaymentOptions.this); alert.setNegativeButton("Back to Payment Options", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) {} }); alert.setTitle("BarclayCard Payment"); LiveWebView liveWevViewObject = new LiveWebView(M_PaymentOptions.this, redirecturl); alert.setView(liveWevViewObject); alert.show(); 

只需在WebView下添加不可见的EditText

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="invisible" /> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent"/> 

所有这些与焦点相关的解决scheme都不适用于我的Samsung Note 3 / Android 5.0