正如问题所说,它不起作用。当我按下菜单按钮时,没有任何结果。
这是菜单活动:
public class AndroidMenuActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.viewphoto);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.edit:
Toast.makeText(getApplicationContext(), "edit selected", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), PhotoFlashActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
和一个非常小的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/edit"
android:title="Edit"/>
</menu>
我不知道出了什么问题,我看过的教程没有提到将活动添加到清单文件中,所以我不确定是否必须这样做。即使我这样做也不行。我已经尝试清理项目,但这也不起作用。
答案 0 :(得分:0)
这可能不是什么,但是如果你将onCreateOptionsMenu(...)重写为如下所示会发生什么:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
答案 1 :(得分:0)
像这样更改你的代码并chaeck。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onContextItemSelected(MenuItem item) {
onOptionsItemSelected(item);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.edit:
Toast.makeText(getApplicationContext(), "edit selected", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), PhotoFlashActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
答案 2 :(得分:0)
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_custom__list_view, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
....code......
return super.onOptionsItemSelected(item);
}
答案 3 :(得分:0)
尝试使用此代码works
让我完美。
main.class
public class OptionmenuActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
//return super.onCreateOptionsMenu(menu);
menu.add("hello").setIcon(R.drawable.ic_launcher);
menu.add("patel").setIcon(R.drawable.ic_launcher);
menu.add("abc").setIcon(R.drawable.ic_launcher);
menu.add("hello").setIcon(R.drawable.ic_launcher);
menu.add("").setIcon(R.drawable.ic_launcher);
menu.add("").setIcon(R.drawable.ic_launcher);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.feeds:
Toast msg = Toast.makeText(OptionmenuActivity.this, "feeds", Toast.LENGTH_LONG);
msg.show();
return true;
case R.id.friends:
Toast msg1 = Toast.makeText(OptionmenuActivity.this, "friends", Toast.LENGTH_LONG);
msg1.show();
return true;
case R.id.about:
Toast msg2 = Toast.makeText(OptionmenuActivity.this, "about", Toast.LENGTH_LONG);
msg2.show();
return true;
case 2:
Toast msg3 = Toast.makeText(OptionmenuActivity.this, "Menu 1", Toast.LENGTH_LONG);
msg3.show();
return true;
}
return true;
}
}
在menu
文件夹中创建res
后,在menu.xml
文件夹中创建menu
文件夹。
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/feeds"
android:title="Feeds"
android:icon="@drawable/ic_launcher"/>
<item
android:id="@+id/friends"
android:title="Friends"/>
<item
android:id="@+id/about"
android:title="About"/>
答案 4 :(得分:0)
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menuInflater.inflate(R.menu.menu, menu);
menu.findItem(R.id.edit).setTitle("Edit");
}
您也可以尝试使用:
menu.findItem(R.id.edit).setVisible(true);
...然后
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.edit:
//DO SOMETHING
return true;
default:
return super.onOptionsItemSelected(item);
}
}
OR .... 您可以在java上创建菜单:
private static final int MENU_EDIT = 1;
MenuItem itemconfig = menu.add(0, MENU_EDIT , 0, "Edit");
itemconfig.setIcon(android.R.drawable.ic_menu_edit);
itemconfig.setShowAsAction(2);
和onOptionsItemSelected:
case: MENU_EDIT
//DO SOMETHING
return true;