当我显示一个时WebView
,我看不到弹出的软键盘.硬键盘也不起作用!
通常的缺点是什么?
我用来访问的代码WebView
是:
package com.example.blahblah; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; import android.view.KeyEvent; import android.view.Window; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class createAccount extends Activity { private static final String LOG_TAG = "Create Account"; private WebView mWebView; private static final String URL = blah_app.urlSelected+"createAccount.html"; private Handler mHandler = new Handler(); @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); Toast.makeText(createAccount.this, "URL = " +URL, Toast.LENGTH_SHORT).show(); getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.webview); getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); mWebView = (WebView) findViewById(R.id.webview); WebSettings webSettings = mWebView.getSettings(); webSettings.setSavePassword(true); webSettings.setSaveFormData(true); webSettings.setJavaScriptEnabled(true); webSettings.setSupportZoom(true); final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 1000); } }); mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo"); mWebView.clearCache(true); setProgressBarVisibility(true); mWebView.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); } @Override public void onPageFinished(WebView view, String url) { mWebView.loadUrl("javascript:(function () { " + "setVariables("+blah_app.numberSelected+",'"+blah_app.urlSelected+"');" + "})()"); } }); mWebView.loadUrl(URL); } final class DemoJavaScriptInterface { public void setData(String fname, String lname, String gacct, String phone) { SharedPreferences prefCreateAccount = PreferenceManager.getDefaultSharedPreferences(createAccount.this); SharedPreferences.Editor editor = prefCreateAccount.edit(); editor.putString("FirstName", fname); editor.putString("LastName", lname); editor.putString("GmailAcct", gacct); editor.putString("Phone", phone); editor.commit(); } DemoJavaScriptInterface() { } /** * This is not called on the UI thread. Post a runnable to invoke * loadUrl on the UI thread. */ public void clickOnAndroid() { mHandler.post(new Runnable() { public void run() { mWebView.loadUrl("javascript:wave()"); } }); } } /** * Provides a hook for calling "alert" from javascript. Useful for * debugging your javascript. */ final class MyWebChromeClient extends WebChromeClient { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(LOG_TAG, message); result.confirm(); return true; } } public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) { startActivity(new Intent(getApplication(), blah_app.class)); return true; } return super.onKeyDown(keyCode, event); } }
小智.. 46
完整的解决方案比Sana更多.它被记录为Android网站上的错误(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; } });
Sana.. 37
问题是webview在加载时没有得到焦点因此使用
webView.requestFocus(View.FOCUS_DOWN);
解决了这个问题.
完整的解决方案比Sana更多.它被记录为Android网站上的错误(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; } });
问题是webview在加载时没有得到焦点因此使用
webView.requestFocus(View.FOCUS_DOWN);
解决了这个问题.
我很惊讶,即使在Android 4.1(在SGS3上测试)问题仍然存在,并且覆盖onTouch()并不能解决它.
测试代码:
String HTML = "TEST"; WebView wv = new WebView(getContext()); wv.loadData(HTML, "text/html", null); final AlertDialog.Builder adb = new AlertDialog.Builder(getContext()); adb.setTitle("TEST").setView(wv).show();
我的复杂解决方案是用MyWebView替换WebView:
private static class MyWebView extends WebView { public MyWebView(Context context) { super(context); } // Note this! @Override public boolean onCheckIsTextEditor() { return true; } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP: if (!hasFocus()) requestFocus(); break; } return super.onTouchEvent(ev); } }
一线答案,它运作良好
// define webview for browser wb = (WebView) findViewById(R.id.webView1); wb.requestFocusFromTouch();
其中wb是我的对象 webView
有相同的问题,但以上解决方案均无效。这让我惊呆了