当我触摸Android中的菜单项时,有没有办法更改操作栏菜单项的文本?

时间:2021-07-22 10:45:35

When I touch this "Edit" button I want it to change to "Done". Is there a way to do this?

当我触摸这个“编辑”按钮时,我希望它改为“完成”。有没有办法做到这一点?

当我触摸Android中的菜单项时,有没有办法更改操作栏菜单项的文本?

I tried this but it only works when opening the menu on the right side:

我试过这个,但它只在打开右侧菜单时才有效:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    // change the text here.

    return super.onPrepareOptionsMenu(menu);
}

1 个解决方案

#1


0  

I assume that, at some point, you inflate the menu, yes?

我认为,在某些时候,你会夸大菜单,是吗?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.some_menu_layout, menu);
    return true;
}

So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)

所以你可以进入你的菜单(在这种情况下,res / menu / some_menu_layout.xml),找到你想要找到的菜单项的android:id。然后,您应该能够使用menu.findItem(theId).setTitle(“Done”);. (您可能必须将Menu变量保存为本地成员才能执行此操作。)

#1


0  

I assume that, at some point, you inflate the menu, yes?

我认为,在某些时候,你会夸大菜单,是吗?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.some_menu_layout, menu);
    return true;
}

So you can go into your menu (in this case, res/menu/some_menu_layout.xml), and find the android:id of the menu item you want to find. You should then be able to use menu.findItem(theId).setTitle("Done");. (You may have to save your Menu variable as a local member to do this.)

所以你可以进入你的菜单(在这种情况下,res / menu / some_menu_layout.xml),找到你想要找到的菜单项的android:id。然后,您应该能够使用menu.findItem(theId).setTitle(“Done”);. (您可能必须将Menu变量保存为本地成员才能执行此操作。)