ContextMenu上下文菜单
1.ContextMenu菜单的组成部分
- 标题及标题图标
- 菜单内容
- 菜单内容的点击响应事件
2.ContextMenu与OptionMenu的区别
- OptionMenu对应的是activity,一个activity只能拥有一个选项菜单
- ContextMenu对应的是View,每个View都可以设置上下文菜单
3.ContextMenu的创建方法
- 首先给View注册上下文菜单registerForContextMenu()
- 添加上下文菜单内容onCreateContextMenu()
- 设置菜单点击后响应事件onContextItemSelected()
实例一:动态添加
package com.example.demo16;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.ContextMenu;import android.view.MenuItem;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showListView(); } /** * 设置ListView显示内容 */ private void showListView(){ ListView listView = (ListView) findViewById(R.id.listView); ArrayAdapteradapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, getData()); listView.setAdapter(adapter); this.registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //设置Menu显示内容 menu.setHeaderTitle("文件操作"); menu.setHeaderIcon(R.drawable.file); menu.add(1, 100, 1, "复制"); menu.add(1, 101, 1, "剪切"); menu.add(1, 102, 1, "粘贴"); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case 100: Toast.makeText(MainActivity.this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(MainActivity.this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case 102: Toast.makeText(MainActivity.this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } /** * 构造ListView显示的数据 * @return */ private ArrayList getData(){ ArrayList list = new ArrayList (); for(int i=0; i<5; ++i) { list.add("文件" + i); } return list; }}
运行效果:
实例二:使用xml添加
1.在资源目录res下创建menu资源文件夹
2.在menu文件夹下创建main.xml文件
3.创建菜单
package com.example.demo16;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.ContextMenu;import android.view.MenuInflater;import android.view.MenuItem;import android.view.View;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showListView(); } /** * 设置ListView显示内容 */ private void showListView(){ ListView listView = (ListView) findViewById(R.id.listView); ArrayAdapteradapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, getData()); listView.setAdapter(adapter); //注册上下文菜单 this.registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //设置Menu显示内容 menu.setHeaderTitle("文件操作"); menu.setHeaderIcon(R.drawable.file); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_item1: Toast.makeText(MainActivity.this, "点击了复制", Toast.LENGTH_SHORT).show(); break; case R.id.action_item2: Toast.makeText(MainActivity.this, "点击了剪切", Toast.LENGTH_SHORT).show(); break; case R.id.action_item3: Toast.makeText(MainActivity.this, "点击了粘贴", Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } /** * 构造ListView显示的数据 * @return */ private ArrayList getData(){ ArrayList list = new ArrayList (); for(int i=0; i<5; ++i) { list.add("文件" + i); } return list; }}
效果如上例