【问题标题】:Android ImageButton Pressed State Doesn't ChangeAndroid ImageButton 按下状态不会改变
【发布时间】:2016-07-22 20:26:06
【问题描述】:

我正在以编程方式设置 ImageButton 的按下状态。

问题是当我按下它时,状态根本没有改变。我尝试使用state_focused 和其他一些按钮,但按钮根本没有改变。你知道为什么我的 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);

默认状态图像确实显示正确,但按下时没有变化。

【问题讨论】:

    标签: java android android-imagebutton


    【解决方案1】:

    尝试添加这一行:

    _pauseButton.setPressed(boolean)
    

    如果没有帮助,请尝试阅读这篇文章: Android : How to update the selector(StateListDrawable) programmatically

    【讨论】:

    • 谢谢,等我回到办公室试试这个
    • 不幸的是设置按下状态不起作用
    【解决方案2】:

    我必须将默认状态设置为 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));
    

    看起来像 android 中的一个错误,我已经设置了一个报告:

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

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 2017-06-22
      • 2018-07-17
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多