Android选项菜单不工作

7gcisfzg  于 2023-05-21  发布在  Android
关注(0)|答案(6)|浏览(142)

正如问题所说,它不起作用。当我按下菜单按钮时,什么也没有出来。
这是菜单“活动:

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>

我不知道出了什么问题,我看过的教程没有提到将活动添加到清单文件中,所以我不确定是否必须这样做。即使我这么做了,它也不起作用。我试过清理项目,但也不起作用。

rbpvctlc

rbpvctlc1#

这可能没什么,但是如果你重写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); 
}
3phpmpom

3phpmpom2#

试试这个代码,它的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文件夹中创建menu.xml后,将menu文件夹创建到res文件夹中。
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"/>
cnwbcb6i

cnwbcb6i3#

@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);
        }
    }

或者....你可以在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;
d4so4syb

d4so4syb4#

setSupportActionBar(mToolbar);
 getSupportActionBar().setDisplayHomeAsUpEnabled(true);

将此添加到工具栏

wd2eg0qa

wd2eg0qa5#

像这样修改你的代码并检查。

@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);
   }
 }
}
6tr1vspr

6tr1vspr6#

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);
   }

相关问题