动作条的onclick菜单项? [英] actionbar menu item onclick?
问题描述
我有一个动作条,把一切都在一个菜单中的右上角,这对用户的点击和菜单选项打开。
I have an action bar that puts everything in a menu in the top right, which the user clicks and the menu options open up.
我膨胀的操作栏菜单,这对每一项活动我使用它:
I inflate the action bar menu with this on each activity I use it:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main2, menu);
return true;
}
和我的XML的main2.xml是:
And my xml for main2.xml is:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:title="Seach"/>
</menu>
我的问题是我把一个onclick在该项目中的XML,如果是这样我在哪里把它调用的onclick方法?我需要把它放在每一次活动我推出这个动作吧?
My question is do I put an onclick in the item in the xml and if so where do I put the onclick method it calls? Do I need to put it in every activity I launch this action bar in?
推荐答案
如果你添加一个onclick属性上这样你的菜单项:
If you add an onClick attribute on your menu item like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_searchHome"
android:orderInCategory="100"
android:showAsAction="never"
android:onClick="doThis"
android:title="Seach"/>
</menu>
然后在您的活动:
Then in your activity:
public void doThis(MenuItem item){
Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
}
注意:
记住,如果你使用的是ActionBarSherlock库,你将不得不进口的
进口com.actionbarsherlock.view.MenuItem;
,而不是的
进口com.android.view.MenuItem;
另外,你可以做这样的事情:的ActionBar福尔摩斯菜单项的OnClick
In addition, you could do something like this: ActionBar Sherlock Menu Item OnClick
这@adneal提及。
which @adneal mentions.
这篇关于动作条的onclick菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!