Android Targetsdk23如何通过自定义按钮打开菜单?

时间:2016-05-03 10:26:47

标签: android compatibility

在Google取消菜单按钮后,有些手机没有按钮,调整A我使用下面的代码仍然没有成功打开菜单,我做错了什么?

    code:
  public void menu(View v)
    {
        KeyEvent event=new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
        MainActivity.this.dispatchKeyEvent(event);
    }
and the gradle:
minSdkVersion 18
targetSdkVersion 23
i also tried :
myActivity.this.openOptionsMenu();

主要活动代码:

    public class MainActivity extends AppCompatActivity {

        private int openfileDialogId = 0;
        private Button offline_button = null;
        private statisticShow ss;
        private String filepath;
        private xlsAndXlsxInput input;
        private final static int ItemIndex = Menu.FIRST+1;
        private Button menu;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //       requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            offline_button = (Button) findViewById(R.id.offline_button);
            menu = (Button)findViewById(R.id.menubutton);
            menu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openOptionsMenu();
                }
            });
            getSupportActionBar().hide();
            SpeechUtility.createUtility(MyApplication.getMyContext(), SpeechConstant.APPID+"=5715fff3");
        }

        public void toOffline(View v) {
            final EditText et = new EditText(this);
            et.setBackgroundColor(Color.WHITE);
            et.setHint("please input the number");
            et.setHintTextColor(Color.GRAY);
            new AlertDialog.Builder(this)
                    .setView(et)
                    .setPositiveButton("sure", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Intent tooffline = new Intent(MainActivity.this, OfflineActivity.class);
                    tooffline.putExtra("classname", et.getText().toString());
                    startActivity(tooffline);
                }

            })
                    .setNegativeButton("cancel", null)
                    .show();

        }

        public void toOnline(View v) {
            Intent online = new Intent(this, OnlineActivity.class);
            startActivity(online);
        }

        public void createExcel(View v) {
            Intent create = new Intent(this, AddStudent.class);
            startActivity(create);
        }

        public void imports(View v) {
            showDialog(openfileDialogId);
        }

        @Override
        protected Dialog onCreateDialog(int id) {
            if (id == openfileDialogId) {
                Map<String, Integer> images = new HashMap<String, Integer>();
                images.put(OpenFileDialog.sRoot, R.drawable.filedialog_root);    
                images.put(OpenFileDialog.sParent, R.drawable.filedialog_folder_up);    
                images.put(OpenFileDialog.sFolder, R.drawable.filedialog_folder);   
                images.put("xls", R.drawable.filedialog_xlsfile);   
                images.put(OpenFileDialog.sEmpty, R.drawable.filedialog_root);
                Dialog dialog = OpenFileDialog.createDialog(id, this, "open the files", new CallbackBundle() {
                            @Override
                            public void callback(Bundle bundle) {
                                filepath = bundle.getString("path");
                                input = new xlsAndXlsxInput(new File("mnt" + File.separator + filepath));
                            }
                        },
                        ".xls;",
                        images);
                return dialog;
            }
            return null;
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(0,ItemIndex,0,"imformations");
            menu.add(0,ItemIndex+1,0,"about");
            menu.add(0,ItemIndex+2,0,"help");
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item)
        {
            switch(item.getItemId())
            {
                case ItemIndex:
                    final EditText et = new EditText(this);
                    new AlertDialog.Builder(this)
                            .setTitle("please input the number")
                            .setView(et)
                            .setPositiveButton("sure", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                        Intent list = new Intent(MainActivity.this, ListActivity.class);
                                        list.putExtra("classname",et.getText().toString());
                                        startActivity(list);

                                }
                            })
                            .setNegativeButton("cancel", null)
                            .show();
                    break;
                case ItemIndex+1:
                    Intent about = new Intent(MainActivity.this, ShowActivity.class);
                    startActivity(about);
                    break;
                case ItemIndex+2:
                    Intent help = new Intent(MainActivity.this, HelpActivity.class);
                    startActivity(help);
                    break;
            }
            return super.onOptionsItemSelected(item);
        }
}

1 个答案:

答案 0 :(得分:0)

使用此

yourbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    openOptionsMenu();
}
});