操作栏:让视图元素 [英] Action bar : get view element

查看:109
本文介绍了操作栏:让视图元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行操作栏成分,它

我的 RES /菜单/ action_menu.xml 持有上显示两个项目的操作栏

my res/menu/action_menu.xml which holds two items to be shown on Action Bar:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item 
          android:id="@+id/help_me"
          android:icon="@drawable/help"
          android:showAsAction="always"/>

    <item 
          android:id="@+id/log_out"
          android:icon="@drawable/logout"
          android:showAsAction="always"/>

</menu>

在我的活动

public class MyActivity extends FragmentActivity{

     @Override
     protected void onCreate(Bundle arg0) {
        super.onCreate(arg0); 
        ...
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_menu, menu);
        return true;
    }

}

我想来从操作栏中的&LT的 @ + ID / help_me 图标;项目> 添加 onClickListener 来它的基础上,在练习我的上述code,在哪里以及如何可以我取 @ + ID / help_me 图标,并添加监听器??

I would like to fetch the @+id/help_me icon from action bar < item > and add onClickListener to it, based on my above code in Activity, where and how can I fetch the @+id/help_me icon and add the listener ??

推荐答案

您可以使用 menu.findItem(R.id.help_me)和<一个href=\"http://developer.android.com/reference/android/view/MenuItem.html#setOnMenuItemClickListener%28android.view.MenuItem.OnMenuItemClickListener%29\"相对=nofollow> OnMenuItemClickListener 。记住,除非你做什么特别的菜单项,你应该使用标准的回调<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onOptionsItemSelected%28android.view.MenuItem%29\"相对=nofollow>内置到活动

这篇关于操作栏:让视图元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆