ContextMenu上下文選單

NO IMAGE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/layout"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<ListView android:id="@ id/lv_list" android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
</RelativeLayout>
<!--此佈局檔案定義了listview,用來顯示一組列表,稍後上下文選單將被註冊到該listview,並且響應該listview。-->
/contextMenu/res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@ id/new_game" android:title="新遊戲" />
<item android:id="@ id/help" android:title="幫助" />
<item android:id="@ id/addnew" android:title="新增新遊戲" />
<item android:id="@ id/delete" android:title="刪除遊戲" />
</menu>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/layout"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<TextView android:id="@ id/tv" android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textSize="20dip"
>
</TextView>
</RelativeLayout>
package com.test.contextmenu;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
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.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class DemoActivity extends Activity {
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("item", "第一個選單項");
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("item", "第二個選單項");
HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("item", "第三個選單項");
HashMap<String, String> map4 = new HashMap<String, String>();
map4.put("item", "第四個選單項");
HashMap<String, String> map5 = new HashMap<String, String>();
map5.put("item", "第五個選單項");
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
//初始化該控制元件
lv=(ListView)findViewById(R.id.lv_list);
//定義介面卡 引數為該顯示資訊用的佈局檔案 以及顯示資訊用的控制元件,這裡是一個textview控制元件
SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.item, new String[]{"item"}, new int[]{R.id.tv});
lv.setAdapter(adapter);
//註冊上下文選單顯示用的view
registerForContextMenu(this.lv);
}
//當選單某個選項被點選時呼叫該方法
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info= (AdapterContextMenuInfo) item.getMenuInfo();
System.out.println(info.position);
System.out.println(info.id);
switch(item.getItemId())
{
case R.id.help:
Toast.makeText(this, "請求幫助", Toast.LENGTH_LONG).show();
return true;
case R.id.addnew:
Toast.makeText(this, "新增新的", Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
Toast.makeText(this, "刪除資訊", Toast.LENGTH_LONG).show();
return true;
case R.id.new_game:
Toast.makeText(this, "新遊戲", Toast.LENGTH_LONG).show();
return true;
default:
}
return super.onContextItemSelected(item);
}
//當上下文選單關閉時呼叫的方法
@Override
public void onContextMenuClosed(Menu menu) {
System.out.println("close contextMenu");
super.onContextMenuClosed(menu);
}
}