mianactivity.java
package com.example.menusample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_popup_menu);
btn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
PopupMenu popupMenu = new PopupMenu(MainActivity.this, btn);
popupMenu.getMenuInflater().inflate(R.menu.main, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item)
{
// TODO Auto-generated method stub
return false;
}
});
popupMenu.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 1001, 1, "启动Activity");
menu.add(0, 1002, 2, "第二项1");
SubMenu subMenu = menu.addSubMenu(0, 1003, 3, "有子菜单");
subMenu.add(0, 2001, 1, "子项一");
subMenu.add(0, 2002, 2, "子项二");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
switch (id)
{
case 1001:
{
Intent intent = new Intent(MainActivity.this, ContextMenuActivity.class);
startActivity(intent);
break;
}
case 1002:
Toast.makeText(MainActivity.this, item.getTitle() + "id:" + id, Toast.LENGTH_SHORT).show();
break;
case 2001:
Toast.makeText(MainActivity.this, item.getTitle() + "id:" + id, Toast.LENGTH_SHORT).show();
break;
case 2002:
Toast.makeText(MainActivity.this, item.getTitle() + "id:" + id, Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
context_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.menusample.ContextMenuActivity" >
<item
android:id="@+id/settings_1_1"
android:orderInCategory="1"
android:title="第一项"/>
<item
android:id="@+id/settings_1_2"
android:orderInCategory="1"
android:title="第二项"/>
</menu>
ContextMenuActivity.java
package com.example.menusample;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
public class ContextMenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context_menu);
ListView lvContextMenu = (ListView) findViewById(R.id.lv_context_menu);
// 注册上下文菜单,长按listview即可弹出上下文菜单
registerForContextMenu(lvContextMenu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
// 创建上下文菜单,并绑定上下文菜单布局
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.context_menu, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
}