试试这个示例代码来了解如何使用上下文菜单:
package com.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MenuActivity extends Activity{
private final static int MENU_PARAMETRE = 1;
private final static int MENU_QUITTER = 2;
private final static int SOUSMENU_VIDEO = 11;
private final static int SOUSMENU_AUDIO = 12;
private final static int MENU_CONTEXT_1 = 13;
private final static int MENU_CONTEXT_2 = 14;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
TextView textView = (TextView)findViewById(R.id.textViewClick);
registerForContextMenu(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sousMenu = menu.addSubMenu(0, MENU_PARAMETRE, Menu.NONE, "Paramètres");
sousMenu.setIcon(R.drawable.image1);
sousMenu.add(0, SOUSMENU_AUDIO, Menu.NONE, "Audio");
sousMenu.add(0, SOUSMENU_VIDEO, Menu.NONE, "Video");
menu.add(0, MENU_QUITTER, Menu.NONE, "Quitter");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case MENU_PARAMETRE:
Toast.makeText(this, "Ouverture des paramètres", Toast.LENGTH_SHORT).show();
return true;
case SOUSMENU_AUDIO:
Toast.makeText(this, "Ouverture des Audio", Toast.LENGTH_SHORT).show();
return true;
case SOUSMENU_VIDEO:
Toast.makeText(this, "Ouverture des Video", Toast.LENGTH_SHORT).show();
return true;
case MENU_QUITTER:
this.finish();
return true;
default: return true;
}
}
//on créé un menu contextuel sur la textview
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
switch(v.getId()){
case R.id.textViewClick:
{
menu.setHeaderTitle("Menu Contextuel");
menu.setHeaderIcon(R.drawable.image1);
menu.add(0, MENU_CONTEXT_1, 0, "Menu Contextuel 1");
menu.add(0, MENU_CONTEXT_2, 0, "Menu Contextuel 2");
}
break;
}
super.onCreateContextMenu(menu, v, menuInfo);
}
//On ajoute les actions sur les items du menu contextuel.
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case MENU_CONTEXT_1:{
Toast.makeText(this, "Menu Contextuel 1 cliqué!", Toast.LENGTH_SHORT).show();
break;
}
case MENU_CONTEXT_2:{
Toast.makeText(this, "Menu Contextuel 2 cliqué!", Toast.LENGTH_SHORT).show();
break;
}
}
return super.onContextItemSelected(item);
}
}
并添加:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/textViewClick"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="30dip"
android:gravity="center_horizontal"/>
</LinearLayout>