从头学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 2012-03-26 23:33 vus520 阅读(...) 评论(...) 编辑 收藏

导航

公告