我对 进行子类化WebView
,并在我的活动中覆盖onKeyDown
:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("mxwebviewact", "mxwebactivity onKeyDown...");
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(webView.canGoBack()){
Log.i("mxwebviewact", "webview can go back");
webView.goBack();
return false;
}else{
Log.i("mxwebviewact", "webview cannot go back");
}
}
return super.onKeyDown(keyCode, event);
}
问题webView.canGoBack()
总是返回 false,我无法使用返回按钮返回。
在我的 WebView 类中,我重写了 setWebViewClient
public void setWebViewClient(MobileBankWebViewClient client){
if(client == null){
return;
}
super.setWebViewClient(webViewClient);
}
// constructor
public MXWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MXWebView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MXWebView(Context context) {
super(context);
init();
}
private void init(){
this.setWebViewClient(new MobileBankWebViewClient());
webChromeClient = new MobileBankWebChromeClient();
this.setWebChromeClient(webChromeClient);
this.setScrollBarStyle(0);
WebView.enablePlatformNotifications();
this.requestFocus();
this.setScrollbarFadingEnabled(true);
this.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
WebSettings setting = this.getSettings();
setting.setCacheMode(WebSettings.LOAD_DEFAULT);
setting.setJavaScriptEnabled(true);
setting.setJavaScriptCanOpenWindowsAutomatically(true);
setting.setPluginsEnabled(false);
setting.setDefaultTextEncodingName("UTF-8");
setting.setSupportZoom(false);
setting.setBuiltInZoomControls(false);
setting.setUseWideViewPort(true);
setting.setDomStorageEnabled(true);
setting.setAppCacheMaxSize(8*1024*1024);
setting.setAllowFileAccess(true);
setting.setAppCacheEnabled(true);
setting.setDatabaseEnabled(true);
setting.setLightTouchEnabled(true);
setting.setSavePassword(false);
setting.setSaveFormData(false);
String dir = this.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
setting.setDatabasePath(dir);
setting.setGeolocationDatabasePath(dir);
setting.setGeolocationEnabled(true);
try {
Method setLayerTypeMethod = this.getClass().getMethod("setLayerType", new Class[] {int.class, Paint.class});
setLayerTypeMethod.invoke(this, new Object[] {1, null});
System.out.println("android version >= 3.0");
} catch (NoSuchMethodException e) {
// Older OS, no HW acceleration anyway
System.out.println("android version < 3.0");
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Log.i("xxx", "mxwebview finish init");
}
在 MobileBankWebViewClient 中,我重写了 shouldOverrideUrlLoading
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url != null){
url = url.trim();
}else{
return true;
}
try{
view.loadUrl(url);
return true;
// some other treatment...
}catch(Exception e){
e.printStackTrace();
}
return true;
}
我找不到错误在哪里,请帮助我。