WebView在Fragment中点击返回键返回上一页
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebView在Fragment中点击返回键返回上一页相关的知识,希望对你有一定的参考价值。
参考技术A 在大多数情况下,在Activity中实现WebView点击返回键返回上一页是经常遇到的事情,并且很容易解决,如下:android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">//无滚动条
@Override
protected voidonCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
WebView webView = (WebView)findViewById(R.id.webView);
@Override
booleanonKeyDown(intkeyCode,KeyEvent event)
if(keyCode == KeyEvent.KEYCODE_BACK&&webView.canGoBack())
webView.goBack();
return true;
else
//结束当前页
return super.onKeyDown(keyCode,event);
那么在Fragment中,我们是如何进行webview点击返回键时返回上一级呢?
如下:
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
@Override
publicViewonCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_webview,container, false);
WebView webView = (WebView)view.findViewById(R.id.webView);
webView.setOnKeyListener(newView.OnKeyListener()
@Override
public booleanonKey(View view, intkeyCode,KeyEvent keyEvent)
if((keyCode == KeyEvent.KEYCODE_BACK) &&webView.canGoBack())
getActivity().runOnUiThread(newRunnable()
@Override
public voidrun()
webView.goBack();
);
return true;
return false;
);
returnview;
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 }
以上是关于WebView在Fragment中点击返回键返回上一页的主要内容,如果未能解决你的问题,请参考以下文章
Android中Fragment装载WebView 返回键设置问题