简体   繁体   English

Android ImageButton按下状态不变

[英]Android ImageButton Pressed State Doesn't Change

I am programatically setting the pressed state of an ImageButton. 我以编程方式设置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. 我尝试使用state_focused和其他一些按钮,但是该按钮完全没有改变。 Do you know why my ImageButton is not changing its image when pressed? 您知道为什么我的ImageButton在按下时没有更改其图像吗?

    _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 如果没有帮助,请尝试阅读本文: Android:如何以编程方式更新选择器(StateListDrawable)

I had to set the default state LAST. 我必须将默认状态设置为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: 看起来像android中的错误,我已经设置了一个报告:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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