我想在我的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;
}
}
答案 0 :(得分: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);
}