Android之弹出多级菜单
使用布局文件创建菜单:(多级菜单)
在res下创建目录menu(假设已经有啦就不用再创建了)
在该menu目录下创建XML文件这里我把文件名称命名为menu
在创建的menu.XML文件里
写入:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/file"
android:title="file"/>
<item
android:id="@+id/edit"
android:title="edit"/>
<item
android:id="@+id/help"
android:title="help">
<menu>
<item
android:id="@+id/window"
android:title="window"/>
<item
android:id="@+id/run"
android:title="run"/>
</menu>
</item>
</menu>
在Activity中创建菜单的地方把以下的代码换成this.getMenuInflater().inflate(R.menu.menu,menu);
R.menu.menu代表你创建的菜单XML文件
menu.add("file");
menu.add("edit");
menu.add("source");
即:
public class MainActivity extends Activity {
private TextView username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.username);
password = (TextView) findViewById(R.id.password);
this.registerForContextMenu(username);//注冊菜单
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
this.getMenuInflater().inflate(R.menu.menu,menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//当菜单上下文元素被选中时运行
Toast.makeText(this, item.getTitle()+"onContextItemSelected",
1).show();
return super.onContextItemSelected(item);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item)
{
// TODO Auto-generated method stub
//菜单元素被选中时运行
Toast.makeText(this, item.getTitle()+"onMenuItemSelected",
1).show();
return super.onMenuItemSelected(featureId, item);
}
}
Android之弹出多级菜单的更多相关文章
- 鼠标右键Table的td弹出多级菜单,双击td编辑
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...
- Android之弹出菜单框【注冊上下文菜单】
注冊上下文菜单:(长按弹出一个菜单) 第一种创建方法(与长按事件结合): public class MainActivity extends Activity { private TextView u ...
- ListView的使用(二)长按弹出上下文菜单
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView ...
- EditorGUILayout.EnumPopup 枚举弹出选择菜单
http://www.unity蛮牛.com/thread-25490-1-1.html http://www.unity蛮牛.com/m/Script/EditorGUILayout.EnumPop ...
- 转:jQuery弹出二级菜单
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- WPF:设置弹出子菜单的是否可用状态及效果
需求: 设置弹出子菜单(二级)项仅首项可用,其他项均不可用:不可用是呈灰色效果. 注: 菜单项都是依据层级数据模板.具体格式如下: StackBlock{TextBlock{Image}.TextBl ...
- Android屏幕底部弹出DialogFragment(3)
Android屏幕底部弹出DialogFragment(3) 附录文章1,2的DialogFragment是常规的DialogFragment,但是现在的一些Android开发中,往往需要从底部 ...
- 微信小程序弹出操作菜单
微信小程序弹出操作菜单 比如在页面上放一个按钮,点击按钮弹出操作菜单,那么在按钮的 bindtap 事件里,执行下面的代码即可: wx.showActionSheet({ itemList: ['A' ...
- 如何在 QWidget 窗口上弹出右键菜单
Title : QWidget 窗口上弹出右键菜单的两个方法 Solution 1 : 给一个 QWidget 添加 QActions,设置 QWidget 的 contextMenuPolicy 属 ...
随机推荐
- Gameia
F - Gameia HDU - 6105 Alice and Bob are playing a game called 'Gameia ? Gameia !'. The game goes l ...
- React深入 - 手写redux api
简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...
- HTML5表单新增元素与属性
form属性 在html4中,表单的从属元素必须写在表单内部,而在HTML5中,可以把他们书写在任何地方,然后为该元素指定一个form属性,属性值为该表单的id,这样就可以声明该元素从属于指定表单了. ...
- 第三讲:post-processsing with vcs+ files
1,dump wave by system function $vcdpluson(level_number,module_instance,....|net_or_reg) $vcdplusoff ...
- 剑指Offer(书):二进制中1的个数
题目:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 分析:下面这两种方法都可以,不过第二种更好一些. public int numberOf1(int n) { int count ...
- STM32定时器的两个小难点
TIM1 TIM8 挂在APB2上 一般为72M 也即APB2分频系数为1其余TIMER可以认为都挂在APB1上,一般为36M 也即APB1分频系数为2 或者更大 至少为2 APB1不能超过36M定时 ...
- POJ 1985 Cow Marathon (求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- 【UVA 1636】 (条件概率)
题链:https://cn.vjudge.net/problem/UVA-1636 题意 给出一把枪的子弹序列,已知开了一枪且无子弹,问下一步的决策是旋转,还是开下一枪 题解 已知开一枪没子弹,下一枪 ...
- 【HIHOCODER 1469 】福字(DP)
描述 新年到了,你收到了一副画.你想找到里面最大的福字. 一副画是一个n × n的矩阵,其中每个位置都是一个非负整数. 一个福字被定义成是大小为 k 的正方形,满足其中的每个位置上的数都恰好比他的左边 ...
- JavaScript中上传文件为图片如何读取+JS中如何使用clip()剪切指定区域(UI组件之图片剪裁器)
File读取和FileReader() //获取上传的文件/图片 function getFile(){ var files,len; var reader = new FileReader(); v ...