Android Checkable Menu Item

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

I have the following menu layout in my Android app:




        
相关标签:
9条回答
  • 2020-12-01 02:01

    Answering because the answers here seem long and convoluted.. I have some exact Kotlin code here

    Override your activity at the top and override the function onMenuItemClick, Have a function to handle the button click to open the menu.

    Have an array or list which holds the checked value and sets the check when the menu is re-created

    Note: This code does not keep the menu open, It only ensures that checked items remain checked. I noted there are lots of solutions to that on stack overflow, so have a look at them if that's what you desire

    class exampleActivity : AppCompatActivity(), PopupMenu.OnMenuItemClickListener {
       private var checkChecked = arrayListOf(false,false)
       //some code
    
      fun clickBTN(v: View){
            val popup = PopupMenu(this,v)
            popup.setOnMenuItemClickListener(this)
            popup.inflate(R.menu.yourmenufilename)
            //assuming you have 2 or more menu items
            popup.menu[0].isChecked = checkChecked[0]
            popup.menu[1].isChecked = checkChecked[1]
            popup.show()
      }
    
      override fun onMenuItemClick(item: MenuItem?): Boolean {
         when(item?.itemID){
            R.id.item0 -> {
                    item.isChecked = !item.isChecked
                    checkChecked[0] = item.isChecked
                    return true
            }
            R.id.item1 -> {
                    item.isChecked = !item.isChecked
                    checkChecked[1] = item.isChecked
                    return true
            }
      }
    }
    

    of course in XML you should have your Button and Menu setup. An example menu is here

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/item0"
            android:title="@string/hi"
            android:checkable="true"/>
        <item android:id="@+id/item1"
            android:title="@string/yo"
            android:checkable="true"/>
    </menu>
    
    0 讨论(0)
  • 2020-12-01 02:03

    This may be theme dependant but my menu didn't show a checkbox. I found this :

    Note: Menu items in the Icon Menu cannot display a checkbox or radio button. If you choose to make items in the Icon Menu checkable, then you must personally indicate the state by swapping the icon and/or text each time the state changes between on and off.

    0 讨论(0)
  • 2020-12-01 02:04

    Wrap the items in a group element, like this:

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <group android:checkableBehavior="all">
            <item android:id="@+id/item1"
                  android:titleCondensed="Options"
                  android:title="Highlight Options"
                  android:icon="@android:drawable/ic_menu_preferences">
            </item>
            <item android:id="@+id/item2"
                  android:titleCondensed="Persist"
                  android:title="Persist"
                  android:icon="@android:drawable/ic_menu_preferences"
                  android:checkable="true">
            </item>
        </group>
    </menu>
    

    From the Android docs:

    The android:checkableBehavior attribute accepts either:

    single - Only one item from the group can be checked (radio buttons)

    all - All items can be checked (checkboxes)

    none - No items are checkable

    0 讨论(0)
提交回复
热议问题