简体   繁体   中英

Android ImageButton Pressed State Doesn't Change

I am programatically setting the pressed state of an ImageButton.

Problem is when I press on it, the state does not change at all. I tried using state_focused and a few others but the button does not change at all. Do you know why my ImageButton is not changing its image when pressed?

    _pauseButton = new ImageButton(_context);

    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[] {},
            getResources().getDrawable(R.drawable.pause));
    stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
            getResources().getDrawable(R.drawable.pausepressed));
    _pauseButton.setImageDrawable(stateListDrawable);

The default state image does show up correctly, but when its pressed, there is no change.

Try to add this line:

_pauseButton.setPressed(boolean)

If not help, try to read this article: Android : How to update the selector(StateListDrawable) programmatically

I had to set the default state LAST.

    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
            getResources().getDrawable(R.drawable.pausepressed));
    stateListDrawable.addState(new int[] {},
            getResources().getDrawable(R.drawable.pause));

Looks like a bug in android and I've setup a report:

https://code.google.com/p/android/issues/detail?id=217939&can=4&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM