為ListView創建ContextMenu

ContextMenu稱為上下文菜單,一般在控件上長按時彈出。今天我們學習ContextMenu的用法,這裡與listview相結合,先在ListView顯示幾個Item,然後在Item上長按,彈出一個菜單(就是ContextMenu),點擊菜單上的項目,提示剛才長按的Item的Position。

main.xml文件
Java代碼  <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 
 
    <TextView android:id="@+id/text" android:text="zzz" 
        android:textSize="18sp" android:textColor="#FFFFFF" 
        android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
     
    <ListView android:id="@+id/list" 
        android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    </ListView> 
</LinearLayout> 

菜單有3個條目,在/res/menu/test_menu.xml中定義,和OptionMenu差不多,隻是沒有Icon
Java代碼  package com.hello.menu; 
 
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.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 
 
public class ApiDemos extends Activity { 
    /** Called when the activity is first created. */ 
     
    private TextView mTextView; 
    private ListView mListView; 
    private String [] values; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        mTextView = (TextView) findViewById(R.id.text); 
 
        //下面的代碼用於初始化一些數據 
        values = new String[30]; 
        StringBuilder sb = new StringBuilder("z"); 
        for (int i = 0; i < values.length; i++) { 
            sb.append(i); 
            values[i] = sb.toString(); 
        } 
 
        mListView = (ListView) findViewById(R.id.list); 
        mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values)); 
        mTextView.setText(String.valueOf(mListView.getAdapter().getCount())); 
         
        //為ListView註冊ContextMenu 
        registerForContextMenu(mListView); 
    } 
 
    //創建ContextMenu 
    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenuInfo menuInfo) { 
        // TODO Auto-generated method stub 
        if (v.getId() == R.id.list) { 
            MenuInflater inflater = getMenuInflater(); 
            menu.setHeaderTitle("contextView sample").setHeaderIcon(R.drawable.icon); 
            inflater.inflate(R.menu.test_menu, menu); 
        } 
        super.onCreateContextMenu(menu, v, menuInfo); 
    } 
     
    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
        // TODO Auto-generated method stub 
        if (item.getMenuInfo() instanceof AdapterContextMenuInfo) { 
            AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); 
                //處理菜單的點擊事件 
            switch (item.getItemId()) { 
            case R.id.call: 
                mTextView.setText(item.getTitle().toString() + menuInfo.position); 
                break; 
            case R.id.cc: 
                        mTextView.setText(item.getTitle().toString() + menuInfo.position); 
                break; 
            case R.id.poweroff : 
                        mTextView.setText(item.getTitle().toString() + menuInfo.position); 
                break; 
            } 
             
        } 
        return super.onContextItemSelected(item); 
    } 

 

 摘自 wangjia55的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *