无法解析onKeyDown方法

时间:2015-07-06 09:47:52

标签: android webview

我想创建一个后退选项(onKeyDown),但我对@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the Back button and if there's history if ((keyCode == KeyEvent.KEYCODE_BACK) && wv7.canGoBack()) { wv7.goBack(); return true; } // If it wasn't the Back key or there's no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); } 有疑问。

Cannot resolve method 'onKeyDown(int, android.view.KeyEvent)'

我有这个错误:public class vod extends Fragment { private WebView wv7; public static final String TAG = "VOD"; private VideoEnabledWebView webView; private VideoEnabledWebChromeClient webChromeClient; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.vod, container, false); // Set layout //rootView.findViewById(R.layout.vld); // Save the web view wv7 = (VideoEnabledWebView) rootView.findViewById(R.id.webView7); // Initialize the VideoEnabledWebChromeClient and set event handlers View nonVideoLayout = rootView.findViewById(R.id.nonVideoLayout); // Your own view, read class comments ViewGroup videoLayout = (ViewGroup) rootView.findViewById(R.id.videoLayout); // Your own view, read class comments View loadingView = inflater.inflate(R.layout.vod2, null); // Your own view, read class comments webChromeClient = new VideoEnabledWebChromeClient(nonVideoLayout, videoLayout, loadingView, webView) // See all available constructors... { // Subscribe to standard events, such as onProgressChanged()... @Override public void onProgressChanged(WebView view, int progress) { // Your code... } }; webChromeClient.setOnToggledFullscreen(new VideoEnabledWebChromeClient.ToggledFullscreenCallback() { @Override public void toggledFullscreen(boolean fullscreen) { // Your code to handle the full-screen change, for example showing and hiding the title bar. Example: if (fullscreen) { WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes(); attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN; attrs.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; getActivity().getWindow().setAttributes(attrs); if (android.os.Build.VERSION.SDK_INT >= 14) { getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); } } else { WindowManager.LayoutParams attrs = getActivity().getWindow().getAttributes(); attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN; attrs.flags &= ~WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON; getActivity().getWindow().setAttributes(attrs); if (android.os.Build.VERSION.SDK_INT >= 14) { getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); } } } }); wv7.setWebChromeClient(webChromeClient); // Navigate everywhere you want, this classes have only been tested on YouTube's mobile site wv7.loadUrl("XX"); return rootView; } @Override public void onResume() { super.onResume(); wv7.onResume(); } @Override public void onPause() { super.onPause(); wv7.onPause(); } @Override public void onBackPressed() { if (wv7.canGoBack()) { wv7.goBack(); return; } super.onBackPressed(); } }

你能帮我解决吗? 非常感谢你。

编辑 - 全班 -

[Section1]
SQL1=select * from <tablename>
SQL2=insert into table <table name>

3 个答案:

答案 0 :(得分:0)

试试这个

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (wv7.canGoBack()) {
                    wv7.goBack();
                } 
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

@Override
public void onBackPressed() {
    if (wv7.canGoBack()) {
        wv7.goBack();
        return;
    }
    super.onBackPressed();
}

答案 1 :(得分:0)

  1. 请在OnCreateView方法内部编写此代码

  2. 这里wv_loader是WebView的ID。

    wv_loader.setOnKeyListener(new View.OnKeyListener() {
         @Override
         public boolean onKey(View v, int keyCode, KeyEvent event) {
             //This is the filter
             if (event.getAction()!=KeyEvent.ACTION_DOWN)
                 return true;
             if (keyCode == KeyEvent.KEYCODE_BACK) {
                 if (wv_loader.canGoBack()) {
                     wv_loader.goBack();
                 } else {
                     (getActivity()).onBackPressed();
                 }
                 return true;
             }
             return false;
         }
     });
    

答案 2 :(得分:0)

我建议尝试这个:

val webSettings = webView.settings
        webSettings.javaScriptEnabled = true
        webSettings.domStorageEnabled = true
        webSettings.setAppCacheEnabled(true)

override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    // Check if the key event was the Back button and if there's history
    if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
        webView.goBack()
        return true
    }
    return super.onKeyDown(keyCode, event)
}