从头学Android之ContexMenu上下文菜单

什么是上下文菜单:

有时候我们也可以叫做快键菜单。例如我们在电脑桌面右键所看到的菜单就是快捷菜单,也叫上下文菜单,叫上下文菜单是因为Context翻译成为上下文的意思

创建上下文ContextMenu菜单的步骤:

1、  覆盖Activity的onCreateContextMenu()方法,调用Menu的add方法添加菜单项
2、  覆盖onContexItemSelected()方法,响应菜单单击事件
3、  调用registerForContexMenu()方法为视力注册上下文菜单

public void onCreateContextMenu (ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo)
参数说明:

menu:需要显示的快捷菜单

v:V是用户选择的界面元素

menuInfo:menuInfo是所选择界面元素的额外信息

说明:这个onCreateContextMenu与onCreateOptionsMenu函数不一样,onCreateOptionsMenu函数仅在选项菜单第一次启动时被调用一次,而onCreateContextMenu函数在每次启动都将会被调用一次。

public boolean onContextItemSelected (MenuItem item)

这个方法和前面的onMenuItemSelected大同小异,在此就不再多说明了

实例1:

 

复制代码
view plaincopyprint?
package com.jiahui.activity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
public class ContextMenuDemoActivity 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 myTxt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTxt = (TextView) findViewById(R.id.mytxt);
// 向TextView控件注册上下文菜单
registerForContextMenu(myTxt);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// 添加菜单项
menu.add(0, ITEM1, 0, "红色背景");
menu.add(0, ITEM2, 0, "绿色背景");
menu.add(0, ITEM3, 0, "白色背景");
}
// 上下文菜单选中事项 通过选中不同的按钮来改变TextView的背景颜色
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ITEM1:
myTxt.setBackgroundColor(Color.RED);
break;
case ITEM2:
myTxt.setBackgroundColor(Color.GREEN);
break;
case ITEM3:
myTxt.setBackgroundColor(Color.WHITE);
break;
}
return true;
}
}
复制代码

 

view plaincopyprint?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/mytxt" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

实例2:用XML创建上下文菜单

需要通过Activity的getMenuInflater()返回一个MenuInflater对象,然后通过MenuInflater对象的inflater()方法指定XML文件的引用

public void inflate (int menuRes, Menu menu)

参数说明:
menuRes:XML文件的引用位置
menu:要显示的菜单

 

复制代码
view plaincopyprint?
package com.jiahui.activity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
public class ContextMenuDemoActivity 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 myTxt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myTxt = (TextView) findViewById(R.id.mytxt);
// 向TextView控件注册上下文菜单
registerForContextMenu(myTxt);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
//XML方式创建的菜单项
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
// 上下文菜单选中事项 通过选中不同的按钮来改变TextView的背景颜色
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case ITEM1:
myTxt.setBackgroundColor(Color.RED);
break;
case ITEM2:
myTxt.setBackgroundColor(Color.GREEN);
break;
case ITEM3:
myTxt.setBackgroundColor(Color.WHITE);
break;
}
return true;
}
}
复制代码

context_menu.xml:

view plaincopyprint?
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/contextMenu1" android:title="XML创建的菜单子项1"></item>
<item android:id="@+id/contextMenu2" android:title="XML创建的菜单子项2"></item>
<item android:id="@+id/contextMenu2" android:title="XML创建的菜单子项3"></item>
</menu>

 

posted on   vus520  阅读(612)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年3月 >
26 27 28 29 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
点击右上角即可分享
微信分享提示