当前位置: 代码迷 >> Android >> android 透过 button 弹出menu
  详细解决方案

android 透过 button 弹出menu

热度:349   发布时间:2016-05-01 20:17:49.0
android 通过 button 弹出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(); //这里可以直接通过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");                }            });

?

  相关解决方案