Android菜单(Menu) - 4

 

4.4.6 Context菜单

Android的Context Menu基本上与Windows系统的右键菜单等同。如果Context Menu已经注册到视图(View)上了,那么在屏幕长按(大约2秒钟)就会弹出Context菜单。

修改activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

	<ListView
	    android:id="@+id/list01" 
	    android:layout_width="fill_parent"
	    android:layout_height="0px" 
	    android:layout_weight="1"/>    
    
    <TextView
        android:id="@+id/footer"
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:padding="4dp"
        android:background="#FF666666"
        android:text="@string/footer" />

</LinearLayout>

新建ListView的单项Layout文件listitem.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="24dip"
    android:padding="8dip" />

然后,我们来编写代码。

在编写ContextMenu时必须实现onCreateContextMenu()。执行点击事件由onContextItemSelected()实现。

代码:

package com.example.testmenu;

import android.os.Bundle;
import android.app.Activity;
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;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

	private static final int MENU_FILE_1 = Menu.FIRST;
	private static final int MENU_EXIT_2 = Menu.FIRST + 1;
	private static final int MENU_EDIT_3 = Menu.FIRST + 2;
	private static final int MENU_DELETE = Menu.FIRST + 3;
	
	String[] phones = {
			"HTC Rezound",  "Samsung Galaxy S II Skyrocket", 
    		"Samsung Galaxy Nexus", "Motorola Droid Razr", 
    		"Samsung Galaxy S", "Samsung Epic Touch 4G", 
    		"iPhone 4S", "HTC Titan"
    		};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ListView listView = (ListView) findViewById(R.id.list01);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,
        		phones);
        
        listView.setAdapter(adapter);
        
        registerForContextMenu(listView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
    	MenuInflater menuInflater = getMenuInflater();
    	menuInflater.inflate(R.menu.my_menu, menu);
        return true;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
    		ContextMenuInfo menuInfo) {
    	// TODO Auto-generated method stub
    	if (v.getId() == R.id.list01) {
    		AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    		
    		menu.setHeaderTitle(phones[info.position]);
    		menu.add(0, MENU_EDIT_3, 0, R.string.edit);
    		menu.add(0, MENU_DELETE, 0, R.string.delete);
    	}
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	TextView text = (TextView) findViewById(R.id.footer);
    	
    	switch (item.getItemId()) {
		case MENU_EDIT_3:
			text.setText("Edit Selected");
			
			return true;

		case MENU_DELETE:
			text.setText("Delete Selected");
			
			return true;
			
		default:
			
			return super.onContextItemSelected(item);
		}
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	// TODO Auto-generated method stub
    	switch (item.getItemId()) {
		case MENU_FILE_1:
			fileMenu();
			
			break;

		case MENU_EXIT_2:
			exitMenu();

			break;
			
		default:
			break;
		}
    	
    	return false;
    }
    
    private void fileMenu() {
    	// TODO My-generated method stub
    }
    
    private void exitMenu() {
    	// TODO My-generated method stub
    }
    
}

运行效果:


Android菜单(Menu) - 4_第1张图片



你可能感兴趣的:(android,ListView,layout,delete,Motorola,menu)