不叫onMenuItemClick的第一次 - SherlockListActivity不叫、onMenuItemClick、SherlockListActivity

由网友(安朵er)分享简介:我用SherlockListActivity,我创建了动作条有一个项目和放大器;其clickListener当我创建活动后点击它的第一次,不叫onMenuItemClick,但在后先点击每一次点击,工作没有问题为什么?私人无效BuildTopActionBar(){BitmapDrawable BG =(Bitma...

我用SherlockListActivity, 我创建了动作条有一个项目和放大器;其clickListener 当我创建活动后点击它的第一次,不叫onMenuItemClick,但在后先点击每一次点击,工作没有问题

为什么?

 私人无效BuildTopActionBar(){
        BitmapDrawable BG =(BitmapDrawable)getResources()。getDrawable(
                R.drawable.ic_action_bg);
        bg.setTileModeXY(TileMode.REPEAT,TileMode.REPEAT);
        getSupportActionBar()setBackgroundDrawable(BG)。

        this.getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
        this.getSupportActionBar()setHomeButtonEnabled(真)。
        this.getSupportActionBar()的setTitle(书签)。
    }

    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            //这就是所谓的时候,家里(上)按钮pssed在操作栏$ P $。
            完();
            返回true;
        案例add_bookmark_item:
            // 加
            item.setOnMenuItemClickListener(新OnMenuItemClickListener(){

                公共布尔onMenuItemClick(菜单项项){
                    customAmbilWarnaDialog对话框=新customAmbilWarnaDialog(
                            BookMarksActivity.this,为0xffffffff,
                            新OnAmbilWarnaListener(){

                                公共无效OnCancel的(
                                        customAmbilWarnaDialog对话框){
                                }

                                公共无效的OnOK(customAmbilWarnaDialog对话框,
                                        INT的颜色,字符串名称,诠释hasName){
                                    myDbHelper.AddNewBook​​mark(姓名,
                                            currentPageNum,颜色,hasName);
                                    // code
                                    lv.smoothScrollToPosition((lv.getCount() -  1));
                                }
                            });
                    dialog.show();
                    返回true;
                }
            });

        }

        返回super.onOptionsItemSelected(项目);

    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){

        menu.add(0,add_bookmark_item,0,添加书签)
                .setIcon(R.drawable.bookmark_add)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                                | MenuItem.SHOW_AS_ACTION_ALWAYS);

        返回true;
    }
 

解决方案

的原因是,我们在调用 item.setOnMenuItemClickListener(新OnMenuItemClickListener(){... onOptionsItemSelected()方法。

这意味着它将被设置在第一时间点击菜单项(然后重置每一次点击)和里面的code将只运行一次,你'已经点击它至少一次。

或者调用这 onCreateOptionsMenu()或只需移动从 onMenuItemClick的code()的开关/箱

再捐500万 台籍晋商巨子郭台铭已在家乡投资146亿,他还要协助晋城打造 世界光谷

I use SherlockListActivity, I created ActionBar with one item & its clickListener when i click on it for first time after creating activity, doesn't call onMenuItemClick, but in every click after first click, work with no problem

Why ?

private void BuildTopActionBar() {
        BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
                R.drawable.ic_action_bg);
        bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
        getSupportActionBar().setBackgroundDrawable(bg);

        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.getSupportActionBar().setHomeButtonEnabled(true);
        this.getSupportActionBar().setTitle("bookmarks");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This is called when the Home (Up) button is pressed in the Action Bar.
            finish();
            return true;
        case add_bookmark_item:
            // add
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                public boolean onMenuItemClick(MenuItem item) {
                    customAmbilWarnaDialog dialog = new customAmbilWarnaDialog(
                            BookMarksActivity.this, 0xffffffff,
                            new OnAmbilWarnaListener() {

                                public void onCancel(
                                        customAmbilWarnaDialog dialog) {
                                }

                                public void onOk(customAmbilWarnaDialog dialog,
                                        int color, String name, int hasName) {
                                    myDbHelper.AddNewBookmark(name,
                                            currentPageNum, color, hasName);
                                    // code
                                    lv.smoothScrollToPosition((lv.getCount() - 1));
                                }
                            });
                    dialog.show();                  
                    return true;
                }
            });

        }

        return super.onOptionsItemSelected(item);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        menu.add(0, add_bookmark_item, 0, "add bookmark")
                .setIcon(R.drawable.bookmark_add)
                .setShowAsAction(
                        MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
                                | MenuItem.SHOW_AS_ACTION_ALWAYS);

        return true;
    }

解决方案

the reason is that you're calling the item.setOnMenuItemClickListener(new OnMenuItemClickListener() {... in the onOptionsItemSelected() method.

This means it will be set the first time you click the menuItem (and then reset on every click) and the code inside it will only run once you've clicked it at least once.

either call this in onCreateOptionsMenu() or simply move the code from onMenuItemClick() to the switch/case

阅读全文

相关推荐

最新文章