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


<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);
}

你可能感兴趣的:(android)