0

我想在我的 webview 运行时单击菜单按钮时调用我的函数 onCreateOptionsMenu 。现在,当您在 Webview 运行时单击 Memnu 按钮时,它会显示一些默认菜单,而不是我的自定义菜单。

我将如何修改下面的代码以禁用默认菜单并显示我的自定义菜单?当 Web 视图未运行时,它会在您单击菜单按钮时显示我的自定义菜单。

public class WebViewActivity extends Activity {
    WebView mWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        final Activity mActivity = this;
        super.onCreate(savedInstanceState);

        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        // SETS DEFAULT MODE TO LANDSCAPE
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        // Makes Progress bar Visible
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://Google.com");

        mWebView.setWebChromeClient(new WebChromeClient() {

            public void onProgressChanged(WebView view, int progress) {
                mActivity.setTitle("Google.com");
                mActivity.setProgress(progress * 100);
            }
        });
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) { 
    if (keyCode == KeyEvent.KEYCODE_MENU) { 
        // do your menu stuff here 

        return true; 
    } 
        else 
        return super.onKeyDown(keyCode, event); 
    } 


        @Override
        public boolean onCreateOptionsMenu(Menu menu){
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.mymenu, menu);
            return true;
        }
}
4

1 回答 1

1

您必须覆盖 onKeyDown 函数,才能拦截 Menu 键。否则 webview 将消耗它。像这样的东西:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // do your menu stuff here

        return true;
    }
        else
        return super.onKeyDown(keyCode, event);
}
于 2012-02-29T16:34:45.120 回答