Browse Source

webview按键监听

weizhengliang 1 year ago
parent
commit
b9cc9fce54

+ 30 - 7
app/src/main/java/com/example/informationkanban/MainActivity.java

@@ -147,6 +147,12 @@ public class MainActivity extends AppCompatActivity {
                 //返回true
                 return true;
             }
+
+            /*@Override
+            public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
+                return true;
+                //return super.shouldOverrideKeyEvent(view, event);
+            }*/
         });
 
         webView.setWebChromeClient(new WebChromeClient() {
@@ -159,16 +165,33 @@ public class MainActivity extends AppCompatActivity {
             }
         });
 
+        /*webView.setOnKeyListener(new View.OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                Log.e(TAG, "webview onkey: " + event.getKeyCode() + ", " + event.getAction() + ", can back: " + webView.canGoBack() + ", can forward: " + webView.canGoForward());
+                if (event.getAction() == KeyEvent.ACTION_DOWN) {
+                    if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT && webView.canGoBack()) {
+                        //遥控器左键
+                        webView.goBack();
+
+                        return true;
+                    } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT && webView.canGoForward()) {
+                        //遥控器右键
+                        webView.goForward();
+
+                        return true;
+                    }
+                }
+
+                return false;
+            }
+        });*/
     }
 
-    //点击返回上一页面而不是退出浏览器
     @Override
-    public boolean onKeyDown(int keyCode, KeyEvent event) {
-        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
-            webView.goBack();
-            return true;
-        }
-        return super.onKeyDown(keyCode, event);
+    public void onBackPressed() {
+        Log.e(TAG, "not support go back");
+        //禁用返回
     }
 
     /**

+ 1 - 0
app/src/main/java/com/example/informationkanban/utils/NetFunctionConfig.java

@@ -25,6 +25,7 @@ public class NetFunctionConfig {
     private static final String default_server_ip = "172.28.100.100";
     //private static final String default_server_ip = "8.129.220.143";
     //private static final String default_server_ip = "119.23.151.229";
+    //private static final String default_server_port = "8101";
     private static final String default_server_port = "8100";
 
     public static void setWebviewType(int type) {

+ 5 - 4
app/src/main/res/layout/activity_main.xml

@@ -1,6 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:app="http://schemas.android.com/apk/res-auto"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -9,6 +8,8 @@
     <WebView
         android:id="@+id/wv_webview"
         android:layout_width="match_parent"
-        android:layout_height="match_parent" />
+        android:layout_height="match_parent"
+        android:focusable="true"
+        android:focusableInTouchMode="true"/>
 
-</RelativeLayout>
+</LinearLayout>