【Android之弹出菜单框注冊上下文菜单】古人已用三冬足,年少今开万卷余。这篇文章主要讲述Android之弹出菜单框注冊上下文菜单相关的知识,希望能为你提供帮助。
注冊上下文菜单:(长按弹出一个菜单)
第一种创建方法(与长按事件结合):
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);
username.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add("
file"
);
menu.add("
edit"
);
menu.add("
source"
);
}
});
}
@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);
}
}
另外一种方法:
(注冊上下文菜单)
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
menu.add("
file"
);
menu.add("
edit"
);
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);
}
}
推荐阅读
- 微擎app端上传图片后删除不了图片
- 推荐扔物线的HenCoder Android 开发进阶系列 后期接着更新
- Android ToolBar 使用完全解析
- Android UI经常使用实例 怎样实现欢迎界面(Splash Screen)
- 为什么默认需加android.intent.category.DEFAULT
- Android 通过OnScrollListener来监听RecyclerView的位置
- Android 如何获取Android RecyclerView滑动的距离
- 转载-android studio 各种问题
- GHO文件 雨林木风Win10系统下载2016年11月出品