Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
961 views
in Technique[技术] by (71.8m points)

add Checkbox on Action Bar Android

I've a problem when i was adding Check box on Action Bar, it is not showing Check box on Action Bar only showing checkbox title.

`

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_delete"
    android:icon="@drawable/ic_action_discard"
    android:showAsAction="always"
    android:title=""/>
<item
    android:id="@+id/check_all"
    android:actionViewClass="android.widget.checkbox"
    android:showAsAction="always"
    android:title="@string/action_check"/>
<item
    android:id="@+id/action_add"
    android:icon="@drawable/ic_action_add"
    android:showAsAction="always"
    android:title="@string/action_add"/>

` this is my menu.XML

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Accepted answer not working for me. Checkable tag did the trick:

<item
    android:id="@+id/check_all"
    android:checkable="true"
    android:showAsAction="always"
    android:title="@string/action_check"/>

And then in the Activity who inflates it you should have something like this in order to reflect the change of the checked state (otherwise it won't change the selected state even if you press it):

public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        case R.id.check_all:
            item.setChecked(!item.isChecked());

            return true;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...