menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/item1" android:title="add" android:icon="@android:drawable/ic_menu_save"></item>
<item android:id="@+id/item2" android:title="del2" android:icon="@android:drawable/ic_delete"></item> <item android:id="@+id/item3" android:title="del3" android:icon="@android:drawable/ic_delete"></item>
<item android:id="@+id/item4" android:title="del4" android:icon="@android:drawable/ic_delete"></item>
<item android:id="@+id/item5" android:title="del5" android:icon="@android:drawable/ic_delete"></item>
<item android:id="@+id/item6" android:title="del6" android:icon="@android:drawable/ic_delete"></item>
<item android:id="@+id/item7" android:title="del7" android:icon="@android:drawable/ic_delete"></item>
<item android:id="@+id/item8" android:title="del8" android:icon="@android:drawable/ic_delete"></item> </menu>

java

 /**
* @Title MenuTest.java
* @package com.example.standardview
* @since
* @version 1.0.0
* @author Vic Lee
* @date Aug 26, 2016-2:10:07 PM
*/
package com.example.standardview; import com.example.standardview.R.menu; import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Toast; /**
* @package com.example.standardview
* @typename MenuTest
* @describe create menu
* @version 1.0.0
* @author Vic Lee
* @contact [email protected]
* @date Aug 26, 2016-3:11:24 PM
*/ //public class MenuTest extends Activity {
public class MenuTest extends ListActivity {
ArrayAdapter<String> arrayAdapter;
/* (non-Javadoc)* @see android.app.Activity#onContextItemSelected(android.view.MenuItem)*/
@Override
public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo itemMenuInfo=(AdapterContextMenuInfo) item.getMenuInfo();
//itemMenuInfo.notify();
Toast.makeText(this, "MainID:"+arrayAdapter.getItem(itemMenuInfo.position)+"=>subID:"+item.getTitle(), 3000).show();
return super.onContextItemSelected(item); } /* (non-Javadoc)* @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo)*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.optionmenu, menu);
super.onCreateContextMenu(menu, v, menuInfo); } private String[] mItemStrings=new String[]{"hello1","hello2","hello3"};
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
Toast.makeText(this,item.getTitle().toString() , 3000).show(); return super.onOptionsItemSelected(item); } @Override
public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.optionmenu, menu); //menu.add("hello");
// menu.add(1, 1, 1, "hello");
// menu.add(1, 2, 2, "hello2");
// menu.add(1, 3, 3, "hello3");
// menu.addSubMenu("hello11");
// menu.addSubMenu("hello22");
return super.onCreateOptionsMenu(menu); } @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
arrayAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemStrings);
setListAdapter(arrayAdapter);
registerForContextMenu(getListView());
} }

效果

android Menu-LMLPHP

05-11 08:08