2015-07-06 181 views
1

我想創建一個後退選項(對於webView),但我遇到了問題onKeyDown無法解析方法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(); 
    } 
} 

回答

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(); 
} 
+0

那沒有工作='(我有同樣的問題,我用一個片段。對於我的班級,這是問題嗎? - public class vod extends Fragment { - – Argardor

+1

@Argardor http://stackoverflow.com/questions/10631425/如何添加回去功能在web視圖中片段 –

+0

謝謝你的回覆,但是這並不工作='(我會編輯第一篇文章,並添加我所有的類。 – Argardor