标签:
MainActivity.class
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showlistView(); } private void showlistView() { ListView listView = (ListView) findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getData()); listView.setAdapter(adapter); this.registerForContextMenu(listView);//view注册到上下文菜单 } private ArrayList<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i = 0; i < 5 ;i++){ list.add("ITEM" + i); } return list; } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); //设置上下文菜单显示的内容 menu.setHeaderTitle("文件的操作"); menu.setHeaderIcon(R.mipmap.ic_launcher); //方法一、动态代码加载项目 // menu.add(1, 1, 1, "复制"); // menu.add(1,2,1,"粘贴"); // menu.add(1,3,1,"剪切"); // menu.add(1,4,1,"重命名"); //方法二、静态加载项目 MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_main,menu); } //点击事件 @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()){ case 1:{ Toast.makeText(this,"点击了复制",Toast.LENGTH_SHORT).show(); break; } case 2:{ Toast.makeText(this,"点击了粘贴",Toast.LENGTH_SHORT).show(); break; } case 3:{ Toast.makeText(this,"点击了剪切",Toast.LENGTH_SHORT).show(); break; } case 4:{ Toast.makeText(this,"点击了重命名",Toast.LENGTH_SHORT).show(); break; } } return super.onContextItemSelected(item); } }
标签:
原文地址:http://www.cnblogs.com/zmaibbs7/p/4889695.html