装修队伍做网站,上海中风险地区清零,大学生个人网站模板,网站公司如何推广网站什么是上下文菜单#xff1a;有时候我们也可以叫做快键菜单。例如我们在电脑桌面右键所看到的菜单就是快捷菜单#xff0c;也叫上下文菜单#xff0c;叫上下文菜单是因为Context翻译成为上下文的意思创建上下文ContextMenu菜单的步骤#xff1a;1、 覆盖Activity的onCreat…什么是上下文菜单有时候我们也可以叫做快键菜单。例如我们在电脑桌面右键所看到的菜单就是快捷菜单也叫上下文菜单叫上下文菜单是因为Context翻译成为上下文的意思创建上下文ContextMenu菜单的步骤1、 覆盖Activity的onCreateContextMenu()方法调用Menu的add方法添加菜单项2、 覆盖onContexItemSelected()方法响应菜单单击事件3、 调用registerForContexMenu()方法为视力注册上下文菜单public void onCreateContextMenu (ContextMenu menu,View v,ContextMenu.ContextMenuInfo menuInfo)参数说明menu需要显示的快捷菜单vV是用户选择的界面元素menuInfomenuInfo是所选择界面元素的额外信息说明这个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 version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android
android:orientationvertical android:layout_widthfill_parent
android:layout_heightfill_parent
TextView android:idid/mytxt android:layout_widthfill_parent
android:layout_heightwrap_content android:textstring/hello /
/LinearLayout 实例2用XML创建上下文菜单需要通过Activity的getMenuInflater()返回一个MenuInflater对象然后通过MenuInflater对象的inflater()方法指定XML文件的引用public void inflate (int menuRes, Menu menu)参数说明menuResXML文件的引用位置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 inflatergetMenuInflater();
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:androidhttp://schemas.android.com/apk/res/android
item android:idid/contextMenu1 android:titleXML创建的菜单子项1/item
item android:idid/contextMenu2 android:titleXML创建的菜单子项2/item
item android:idid/contextMenu2 android:titleXML创建的菜单子项3/item
/menu