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

xiaoxiao2021-02-28  113

<?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页面

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //下方代码 webView = (WebView) findViewById(R.id.webView); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if(webView.canGoBack()){ webView.goBack(); return true; } } return super.onKeyDown(keyCode, event); }
转载请注明原文地址: https://www.6miu.com/read-71032.html

最新回复(0)