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); ArrayAdapter<String> adapter = new ArrayAdapter<String>(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<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i=0; i<5; ++i) { list.add("文件" + i); } return list; } }
運行效果:java
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_item1" android:orderInCategory="100" android:showAsAction="never" android:title="複製"/> <item android:id="@+id/action_item2" android:orderInCategory="101" android:showAsAction="never" android:title="剪切"/> <item android:id="@+id/action_item3" android:orderInCategory="102" android:showAsAction="never" android:title="粘貼"/> </menu>
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); ArrayAdapter<String> adapter = new ArrayAdapter<String>(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<String> getData(){ ArrayList<String> list = new ArrayList<String>(); for(int i=0; i<5; ++i) { list.add("文件" + i); } return list; } }
效果如上例android