android 通过 button 弹出 option menu 和 context menu

一,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(); 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"); } });

3,这里只实现长按button弹出Context menu ,可以试一下实现单击实现弹出context menu。

你可能感兴趣的:(android,Class,button,BT,menu)