android - how to get the action bar button? findviewbyid seems doesn't work -
suppose have 2 buttons, 1 action button in action bar(@+id/button1). , common button in layout(@+id/button2).
how can set button1 disabled when click button2?
findviewbyid(button1) seems not work. return null.
this menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/button1" android:title="submit" android:showasaction="always" /> </menu>
and mainacticity:
@override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.my, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.button1 ) { return true; } return super.onoptionsitemselected(item); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my); button1 = (button) findviewbyid(r.id.button1);/*which return null*/ button2 = (button) findviewbyid(r.id.button2); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { button1.setenabled(false);/*what failed do*/ } }); }
1.create variable menuitem in activity class
menuitem menuitem;
2. find variable in oncreateoptionsmenu
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); menuitem = menu.finditem(r.id.item_circuit); return super.oncreateoptionsmenu(menu); }
disable item in buttonclick method
button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { menuitem.setenabled(false); } });
Comments
Post a Comment