Android Checkable Menu Item

后端 未结 9 1826
悲哀的现实
悲哀的现实 2020-12-01 00:59

I have the following menu layout in my Android app:




        
9条回答
  •  旧时难觅i
    2020-12-01 01:40

    I've found that the best solution was to just use the onOptionsItemSelected() method as of my current API (27-28).

    @Override
       public boolean onOptionsItemSelected(MenuItem item) 
       {    
    
    //Copy from here...
           int itemId = item.getItemId();
    
           if(item.isChecked())                          
           { 
               if(R.id.edit_tile_checkbox == itemId)     //Individual checkbox logic
               {   /*TODO unchecked Action*/} 
               item.setChecked(false);                   //Toggles checkbox state.
           }
           else
           {
                if(R.id.edit_tile_checkbox == itemId)    //Individual checkbox logic
                {/*TODO checked Action*/}
                item.setChecked(true);                   //Toggles checkbox state.
           }
    //...To here in to your onOptionsItemSelected() method, then make sure your variables are all sweet.
    
           return super.onOptionsItemSelected(item);
       }
    

    I spent way to long on here for this answer. and for whatever reason, the answers above didn't help (I'm a returning newbie I probably mucked something up I'm sure). There could be a better way of doing this so helpful criticism is welcomed.

提交回复
热议问题