Android 如何在单击其他菜单项时在操作栏上获取菜单项id
所以我在操作栏上有菜单项。在OnOptions ItemSelected上,我想更改菜单项图像 这是我的密码Android 如何在单击其他菜单项时在操作栏上获取菜单项id,android,android-actionbar,menuitem,android-2.3-gingerbread,Android,Android Actionbar,Menuitem,Android 2.3 Gingerbread,所以我在操作栏上有菜单项。在OnOptions ItemSelected上,我想更改菜单项图像 这是我的密码 @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) { case R.id.todaySched:{ viewTodaySched
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.todaySched:{
viewTodaySched();
item.setIcon(R.drawable.calendarselected);
infoLog=(MenuItem)findViewById(R.id.infoLog);
infoLog.setIcon(R.drawable.book);
return true;}
case R.id.infoLog:{
viewInfoLog();
item.setIcon(R.drawable.bookselected);
todaySched=(MenuItem)findViewById(R.id.todaySched);
todaySched.setIcon(R.drawable.calenderselected);
return true;}
default:
return super.onOptionsItemSelected(item);
}
}
但是当我点击图标时,它不会改变,我得到了运行时错误。
e、 g:当我单击todaySched图标时,似乎无法获取信息日志项id
我的日志猫:根据你的日志猫,你得到了类强制转换异常,并且你已经使用了sharlockactionbar。 因此,请尝试并检查您是否导入了正确的菜单项和菜单,如下所示:
import com.actionbarsherlock.view.MenuItem;
and
import com.actionbarsherlock.view.Menu;
而不是
import android.view.MenuItem;
and
import android.view.Menu;
编辑:
以下是只需单击一下即可更改两个图标的方法:
private Menu menu;
private MenuItem item1, item2;
Boolean original = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
this.menu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.todaySched) {
update();
} else if (id == R.id.infoLog) {
update();
}
return true;
}
private void update() {
item1 = menu.findItem(R.id.todaySched);
item2 = menu.findItem(R.id.infoLog);
if (original) {
item1.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_search));
item2.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_report_image));
original = false;
} else if (!original) {
item1.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_my_calendar));
item2.setIcon(getResources().getDrawable(
android.R.drawable.ic_menu_info_details));
original = true;
}
}
检查并正在工作。现在根据您的要求使用它
干杯……每次您想要更改操作栏中的项目时,都必须调用该函数
InvalidateOptions菜单()。然后重写方法public boolean onPrepareOptionsMenu(菜单菜单菜单),在那里您可以获得菜单项,您可以设置图标、创建新操作或删除它们。希望能有所帮助。为什么在诸如
R.id.todaySched:{
?@mohit我刚才提供了关于我的问题的日志。MainActivity的211行是什么?@mohit infoLog=(MenuItem)findViewById(R.id.infoLog);您也可以发布导入吗?我的导入:导入com.actionbarsherlock.app.ActionBar;导入com.actionbarsherlock.app.ActionBar.Tab;导入com.actionbarsherlock.app.SherlockActivity;导入com.actionbarsherlock.view.MenuItem;导入com.actionbarsherlock.view.menuinter;您以前问过这个问题吗?是的是的。在我以前的问题上,我设法更改了当前的“单击”项目。但现在我也想在同一次单击中更改其他项目。更改其他项目是什么意思?请具体说明。