Android以编程方式创建一个简单的菜单

时间:2022-03-09 15:29:32

I'm trying to create a simple menu with one button that will call a method to clear the array. I don't want to use xml because all I need is one button.

我正在尝试使用一个按钮创建一个简单的菜单,该按钮将调用方法来清除数组。我不想使用xml,因为我只需要一个按钮。

Something like this -

像这样的东西 -

public boolean onCreateOptionsMenu(Menu menu) {
    button "Clear Array";
    onClick{// run method that wipes array};
    return true;
}

Thank you

谢谢

3 个解决方案

#1


62  

A--C's method works, but you should avoid setting the click listeners manually. Especially when you have multiple menu items.

A-C的方法有效,但你应该避免手动设置点击监听器。特别是当你有多个菜单项。

I prefer this way:

我更喜欢这种方式:

private static final int MENU_ITEM_ITEM1 = 1;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_ITEM_ITEM1:
        clearArray();
        return true;

    default:
        return false;
  }
}

By using this approach you avoid creating unecessary objects (listeners) and I also find this code easier to read and understand.

通过使用这种方法,您可以避免创建不必要的对象(侦听器),并且我也发现此代码更易于阅读和理解。

#2


25  

Something like this might work:

这样的事情可能有用:

public boolean onCreateOptionsMenu(Menu menu) {
  MenuItem item = menu.add ("Clear Array");
  item.setOnMenuItemClickListener (new OnMenuItemClickListener(){
    @Override
    public boolean onMenuItemClick (MenuItem item){
      clearArray();
      return true;
    }
  });
  return true;
}

Menu gives us a handy method, add(), which allows you to add a MenuItem. So we make one. Then we assign an OnMenuItemClickListener to the MenuItem and override its onMenuItemClick() to do what we want it to do.

Menu为我们提供了一个方便的方法add(),它允许您添加一个MenuItem。所以我们制作一个。然后我们将一个OnMenuItemClickListener分配给MenuItem并覆盖其onMenuItemClick()以执行我们希望它执行的操作。

#3


13  

Programmatically, I was able to create a simple menu using the following code.

以编程方式,我能够使用以下代码创建一个简单的菜单。

private final int MenuItem_EditId = 1, MenuItem_DeleteId = 0;

@Override
  public boolean onCreateOptionsMenu(Menu menu){

    MenuItem edit_item = menu.add(0, MenuItem_EditId, 0, R.string.edit);
    edit_item.setIcon(R.drawable.edit);
    edit_item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    MenuItem delete_item = menu.add(0, MenuItem_DeleteId, 1, R.string.edit);
    delete_item.setIcon(R.drawable.delete);
    delete_item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return super.onCreateOptionsMenu(menu);
}

#1


62  

A--C's method works, but you should avoid setting the click listeners manually. Especially when you have multiple menu items.

A-C的方法有效,但你应该避免手动设置点击监听器。特别是当你有多个菜单项。

I prefer this way:

我更喜欢这种方式:

private static final int MENU_ITEM_ITEM1 = 1;
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(Menu.NONE, MENU_ITEM_ITEM1, Menu.NONE, "Item name");
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_ITEM_ITEM1:
        clearArray();
        return true;

    default:
        return false;
  }
}

By using this approach you avoid creating unecessary objects (listeners) and I also find this code easier to read and understand.

通过使用这种方法,您可以避免创建不必要的对象(侦听器),并且我也发现此代码更易于阅读和理解。

#2


25  

Something like this might work:

这样的事情可能有用:

public boolean onCreateOptionsMenu(Menu menu) {
  MenuItem item = menu.add ("Clear Array");
  item.setOnMenuItemClickListener (new OnMenuItemClickListener(){
    @Override
    public boolean onMenuItemClick (MenuItem item){
      clearArray();
      return true;
    }
  });
  return true;
}

Menu gives us a handy method, add(), which allows you to add a MenuItem. So we make one. Then we assign an OnMenuItemClickListener to the MenuItem and override its onMenuItemClick() to do what we want it to do.

Menu为我们提供了一个方便的方法add(),它允许您添加一个MenuItem。所以我们制作一个。然后我们将一个OnMenuItemClickListener分配给MenuItem并覆盖其onMenuItemClick()以执行我们希望它执行的操作。

#3


13  

Programmatically, I was able to create a simple menu using the following code.

以编程方式,我能够使用以下代码创建一个简单的菜单。

private final int MenuItem_EditId = 1, MenuItem_DeleteId = 0;

@Override
  public boolean onCreateOptionsMenu(Menu menu){

    MenuItem edit_item = menu.add(0, MenuItem_EditId, 0, R.string.edit);
    edit_item.setIcon(R.drawable.edit);
    edit_item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    MenuItem delete_item = menu.add(0, MenuItem_DeleteId, 1, R.string.edit);
    delete_item.setIcon(R.drawable.delete);
    delete_item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return super.onCreateOptionsMenu(menu);
}