Android在web view下点击返回键直接退出程序完美解决

来源:互联网 发布:淘宝网seo 编辑:程序博客网 时间:2024/04/27 19:07
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/q"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:fadingEdgeLength="0sp"    android:orientation="vertical" >//下方代码    <WebView        android:id="@+id/webView"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>

MainActivity.java页面

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //下方代码    webView = (WebView) findViewById(R.id.webView);}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {   if (keyCode == KeyEvent.KEYCODE_BACK) {       if(webView.canGoBack()){           webView.goBack();           return true;       }     }   return super.onKeyDown(keyCode, event);}
原创粉丝点击