首页 > 代码库 > fragment中的WebView返回上一页

fragment中的WebView返回上一页

 1 public final class Text1Fm extends Fragment {  
 2   
 3   
 4     static WebView mWeb;  
 5     private View mContentView;  
 6     private static final String APP_CACAHE_DIRNAME = "/webcache";  
 7   
 8     private Handler handler = new Handler() {  
 9         @Override  
10         public void handleMessage(Message message) {  
11             switch (message.what) {  
12                 case 1: {  
13                     webViewGoBack();  
14                 }  
15                 break;  
16             }  
17         }  
18     };  
19   
20     private void webViewGoBack() {  
21         mWeb.goBack();  
22     }  
23   
24     @Override  
25     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
26   
27         mContentView = inflater.inflate(R.layout.baiduwebview, null);  
28         mWeb = (WebView) mContentView.findViewById(R.id.wv);  
29   
30   
31         WebSettings settings = mWeb.getSettings();  
32         settings.setJavaScriptEnabled(true);  
33         mWeb.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);  
34         mWeb.getSettings().setSupportZoom(true);  //支持放大缩小  
35         mWeb.getSettings().setBuiltInZoomControls(true);  
36         mWeb.loadUrl("你的网址");  
37         settings.setUseWideViewPort(true);  
38         settings.setLoadWithOverviewMode(true);  
39         mWeb.getSettings().setSaveFormData(true);// 保存表单数据  
40         mWeb.setWebViewClient(new WebViewClient());  
41         String cacheDirPath = getActivity().getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME; //缓存路径  
42   
43         mWeb.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);  //缓存模式  
44         mWeb.getSettings().setAppCachePath(cacheDirPath); //设置缓存路径  
45         mWeb.getSettings().setAppCacheEnabled(true); //开启缓存功能  
46   
47         mWeb.setOnKeyListener(new View.OnKeyListener() {  
48   
49             public boolean onKey(View v, int keyCode, KeyEvent event) {  
50                 if ((keyCode == KeyEvent.KEYCODE_BACK) && mWeb.canGoBack()) {  
51                     handler.sendEmptyMessage(1);  
52                     return true;  
53                 }  
54                 return false;  
55             }  
56   
57         });  
58         return mContentView;  
59     }  
60 }  

 

fragment中的WebView返回上一页