本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


在上一节,我们已经知道了在按MENU键的时候,如何显示选项菜单。但是,除了选项菜单,你也可以显示一个上下文菜单。上下文菜单通常是和activity中的组件相关联的。当用户长按一个组件的时候,它的上下文菜单就会被触发。例如,用户长按一个Button,一个上下文菜单就有可能被显示。

如果想要把一个组件与一个上下文菜单联系在一起,就需要在组件上面调用setOnCreateContextMenuListener()。

下面展示如何显示一个上下文菜单(Context Menu)。

1. 使用之前的工程,修改main.xml。

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />        <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="Click and hold on it" /></LinearLayout>

2. 在MenusActivity.java中添加一些代码。

public class MenusActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                Button btn = (Button) findViewById(R.id.button1);        btn.setOnCreateContextMenuListener(this);    }        @Override    public void onCreateContextMenu(ContextMenu menu, View view,    ContextMenuInfo menuInfo)    {         super.onCreateContextMenu(menu, view, menuInfo);         CreateMenu(menu);    }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        ......    }        @Override    public boolean onOptionsItemSelected(MenuItem item)    {         return MenuChoice(item);    }        private void CreateMenu(Menu menu)    {        ......      }    private boolean MenuChoice(MenuItem item)    {        ......    }}

3. 按F11在模拟器上面调试。当点击Button并维持一段时间后,上下文菜单显出出来了。


在上面的例子中,我们调用了button的setOnCreateContextMenuListener()方法,是button与上下文菜单关联到了一起。

当上下文菜单中了某一项被点击时,onContextItemSelected()这个方法就会被触发。

注意,上下文菜单中的快捷键是不起作用的。如果想要使快捷键起作用,需要调用setQuertyMode()方法。

 private void CreateMenu(Menu menu)    {        menu.setQwertyMode(true);        MenuItem mnu1 = menu.add(0, 0, 0, "Item 1");        {            mnu1.setAlphabeticShortcut('a');            mnu1.setIcon(R.drawable.ic_launcher);        }        ......    }



更多相关文章

  1. Android架构组件-LiveData
  2. Android运行异常一:token android.os.BinderProxy@438e7108 is no
  3. Android中实现ListView横向滑动
  4. 阅读《Android(安卓)从入门到精通》(27)——选项菜单
  5. Android(安卓)ActionBar以及menu的代码设置样式
  6. Android之四大组件、五大存储、七大布局
  7. Android官方文档翻译 十 2.3Styling the Action Bar
  8. Android中的四大组件回顾
  9. android用户界面-组件Widget-常用组件

随机推荐

  1. Android异步处理四:AsyncTask的实现原理
  2. Android(安卓)TableLayout 属性含义
  3. 移动支付之Android(安卓)HCE的基本使用
  4. Android Handler机制2之ThreadLocal
  5. android EditText inputType说明
  6. How to add a new keycode in android
  7. Android架构组件-App架构指南
  8. android studio 怎么做屏幕适配?
  9. LibGDX制作android动态壁纸
  10. 第一章 andrid visdio 安装