一android 通过 button 弹出 option menu:
public class TestOptionsMenu extends Activity implements OnClickListener{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.Button01);
bt.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "test");
return super.onCreateOptionsMenu(menu);
}
@Override
public void openOptionsMenu() {
// TODO Auto-generated method stub
super.openOptionsMenu();
}
@Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.Button01:
openOptionsMenu(); //这里可以直接通过button调出按键menu的菜单
break;
}
}
}
二,android 通过 button 弹出 context menu
1,获取button : Button bt = (Button)findViewById(R.id.Button01);
2,设置button 事件:
bt.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
menu.setHeaderTitle("title");
menu.add(0, 1, 0, "item 1");
menu.add(0, 2, 0, "item 2");
}
});