Android用户界面菜单之上下文菜单(Context Menu)
来源:互联网 发布:三合一旅游网站源码 编辑:程序博客网 时间:2024/06/05 09:55
Android系统为我们提供了丰富的可视化用户界面组件,包括菜单、对话框、按钮、下拉列表等。Android系统借用了Java里的UI设计思想,包括事件响应机制和布局管理。Android系统中的所有UI类都是建立在View和ViewGroup这两个类的基础之上。所有的View的子类称为“Widge”,所有ViewGroup的子类称为“Layout”。View和ViewGroup之间采用了组合设计模式(Composite),可以使得“部分-整体”同等对待。ViewGroup作为布局容器类在最上层,布局容器里面又可以有View和ViewGroup。
菜单的设计在人机交互中可以说是非常人性化的,它提供了不同功能分组展示的能力。Android中菜单分为三种类型:选项菜单(Option Menu)、上下文菜单(Context Menu)和子菜单(Sub Menu)。 下面我们继续上一篇,讲解上下文菜单:
当用户长时间按键不放时,弹出的菜单时上下文菜单。我们经常在Windows中用鼠标右键单击弹出的菜单就是上下文菜单。创建过程如下:
1、覆盖Activity的onCreateContextMenu()方法,调用Menu的add方法添加菜单项(MenuItem)。
2、覆盖onContextItemSelected()方法,相应菜单单击事件。
3、调用registerForContextMenu()方法,为试图注册上下文菜单。
案例:
package com.xiaomo.ui;import com.example.helloworld.R;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.ContextMenu;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ContextMenu.ContextMenuInfo;import android.widget.TextView;public class Context_Menu extends Activity {private static final int ITEM1 = Menu.FIRST;private static final int ITEM2 = Menu.FIRST+1;private static final int ITEM3 = Menu.FIRST+2;private TextView myTextView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);myTextView = (TextView)findViewById(R.id.main_text);//注册上下文菜单registerForContextMenu(myTextView);}@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {//添加菜单项menu.add(0,ITEM1,0,"红色背景");menu.add(0, ITEM2, 0, "绿色背景");menu.add(0,ITEM3,0,"白色背景");}@Overridepublic boolean onContextItemSelected(MenuItem item) {switch (item.getItemId()) {//菜单项1被选择case ITEM1://设置TextView背景色myTextView.setBackgroundColor(Color.RED);break;case ITEM2:myTextView.setBackgroundColor(Color.GREEN);break;case ITEM3:myTextView.setBackgroundColor(Color.WHITE);break;}return true;}}
出处:http://blog.csdn.net/cl05300629/article/details/17961231 作者:伫望碧落
2 0
- Android用户界面菜单之上下文菜单(Context Menu)
- Android之上下文菜单
- Android之上下文菜单(ContextMenu)
- Android用户界面菜单之选项菜单(Option Menu)
- Android用户界面菜单之子菜单(Sub Menu)
- 一起学android之上下文菜单创建(5)
- android基础学习之上下文菜单
- android 上下文菜单Context Menu
- android 上下文菜单Context Menu
- android 上下文菜单Context Menu
- android上下文菜单Context Menu
- android的Context Menu菜单
- 第十五章之上下文菜单
- Android 菜单, option menu 和 context menu (右键菜单)
- Android学习:用户界面UI之菜单(Menu)
- android用户界面——菜单menu
- Context Menu上下文菜单
- Android 的上下文菜单: Context Menu
- 替换SQL字段中的换行符,回车符
- IOS键盘的相关设置(UITextfield)
- 关于资源Resource
- Qt 内存泄露原因及解决办法
- MySQL my.cnf 配置文件注释
- Android用户界面菜单之上下文菜单(Context Menu)
- jquery 判断复选框是否全部选中
- struts2 跳转
- 基于web的android图像处理示例(Win7+Apache+PHP+Matlab+Android)http://blog.csdn.net/nupt123456789/article/detail
- 开发任务分发中的横向分发和纵向分发
- 群发“站内信”的实现
- VS2008中OpenGL环境配置
- win7系统使用engine进行开发报错,“未能加载文件或程序集”
- 网页数据采集:[5]新浪微博api详细教程