gpt4 book ai didi

android - 如何在 onOptionItemSelected 事件后更改 ActionBar 项目中的某些文本?

转载 作者:行者123 更新时间:2023-11-30 03:38:16 24 4
gpt4 key购买 nike

我在我的应用程序中使用 ActionBar,我希望当用户单击按钮时,ActionBar 中的项目应该更改文本。

这是我的 onOptionsItemSelected() 代码:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh:
Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
.show();
finish();
break;
case R.id.lg:

Toast.makeText(getBaseContext(), "ma", Toast.LENGTH_SHORT).show();
break;

case R.id.French:

Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();

break;
case R.id.nerlandais:
Toast.makeText(getBaseContext(), "brahim", Toast.LENGTH_SHORT)
.show();

}

return true;
}

我必须怎么做才能更改另一个项目的项目标题?

示例:当我点击法语项目时,我想更改 nerlandais 项目标题。

最佳答案

如果您想将 MenuItem 的标题点击更改为另一个项目,您可以这样做:

私有(private)字符串 mMenuItemTitle;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem item = menu.findItem(R.id.nerlandais);
item.setText(mMenuItemTitle);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.French:
Toast.makeText(getBaseContext(), "zaki", Toast.LENGTH_SHORT).show();
mMenuItemTitle = "My New Title";
supportInvalidateOptionsMenu();
break;
}

return true;

关于android - 如何在 onOptionItemSelected 事件后更改 ActionBar 项目中的某些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com