这段代码在android模拟器中无法显示出menu,没有报错也运行了但就是不显示menu?
publicclassMenuActivityextendsActivity{/**Calledwhentheactivityisfirstcreated.*/@Over...
public class MenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 定义设置上下文菜单的载体并注册
TextView tv = new TextView(this);
tv.setText("上下文菜单的载体");
registerForContextMenu(tv);
setContentView(tv);
//setContentView(R.layout.main);
}
//创建OptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.populateMenu(menu);
return true;
}
//单击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break; }
return false;
}
private void populateMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(android.R.drawable.ic_menu_send);
}} 展开
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 定义设置上下文菜单的载体并注册
TextView tv = new TextView(this);
tv.setText("上下文菜单的载体");
registerForContextMenu(tv);
setContentView(tv);
//setContentView(R.layout.main);
}
//创建OptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
this.populateMenu(menu);
return true;
}
//单击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 1:
Toast.makeText(this, "删除菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 2:
Toast.makeText(this, "保存菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 3:
Toast.makeText(this, "帮助菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 4:
Toast.makeText(this, "添加菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 5:
Toast.makeText(this, "详细菜单被点击了", Toast.LENGTH_LONG).show();
break;
case Menu.FIRST + 6:
Toast.makeText(this, "发送菜单被点击了", Toast.LENGTH_LONG).show();
break; }
return false;
}
private void populateMenu(Menu menu) {
menu.add(Menu.NONE, Menu.FIRST + 1, 5, "删除").setIcon(android.R.drawable.ic_menu_delete);
menu.add(Menu.NONE, Menu.FIRST + 2, 2, "保存").setIcon(android.R.drawable.ic_menu_edit);
menu.add(Menu.NONE, Menu.FIRST + 3, 6, "帮助").setIcon(android.R.drawable.ic_menu_help);
menu.add(Menu.NONE, Menu.FIRST + 4, 1, "添加").setIcon(android.R.drawable.ic_menu_add);
menu.add(Menu.NONE, Menu.FIRST + 5, 4, "详细").setIcon(android.R.drawable.ic_menu_info_details);
menu.add(Menu.NONE, Menu.FIRST + 6, 3, "发送").setIcon(android.R.drawable.ic_menu_send);
}} 展开
4个回答
展开全部
你的程序需要按MENU 才会有菜单,如果想出现上下文菜单应该覆盖 onCreateContextMenu() 和 onContextItemSelected()方法才对如:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, Menu.FIRST + 7, 0, "Edit");
menu.add(0, Menu.FIRST + 8, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case Menu.FIRST + 7:
Toast.makeText(this, "Edit Menu is clicked", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST + 8:
Toast.makeText(this, "Delete Menu is clicked", Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, Menu.FIRST + 7, 0, "Edit");
menu.add(0, Menu.FIRST + 8, 0, "Delete");
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
switch (item.getItemId()) {
case Menu.FIRST + 7:
Toast.makeText(this, "Edit Menu is clicked", Toast.LENGTH_LONG).show();
return true;
case Menu.FIRST + 8:
Toast.makeText(this, "Delete Menu is clicked", Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
必须俺那个menu键才会显示出来菜单
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
onOptionsItemSelected 只有用户点击Menu键时候才会显示出你所想显示的菜单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public boolean onOptionsItemSelected(MenuItem item) 这个方法后面为什么return false而不是return super...?学习一下.按了menu后会弹出下面的菜单
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询