简体   繁体   English

Android比较ConstantState在4.4 KitKat上不起作用

[英]Android compare ConstantState does not work on 4.4 KitKat

I developed an app and everything works great on my s7 and other devices, but I tested it on an older one running 4.4 kitkat and when the app compares the buttons' colours, in 4.4, it doesn't work. 我开发了一个应用程序,并且在s7和其他设备上一切正常,但是我在运行4.4 kitkat的较旧版本上进行了测试,并且当该应用程序在4.4中比较按钮的颜色时,它不起作用。

This is the piece of code I'm using: 这是我正在使用的代码:

for (Button btn : selectorArrayList) {

        final Button button = btn;

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                /*

                    Check if button color is white or red
                    if white, turn red and activate the
                    selector (category) and if it's red
                    turn white and deactivate the selector
                    category)

                 */

                // I've set the background in the XML Layout as R.color.white

                Drawable pd = (Drawable) button.getBackground();

                if (pd.getConstantState().equals(ContextCompat.getDrawable(getContext(), R.color.white).getConstantState())) {

                ...

At this very point on a newer version of the OS it decides the states are equal, and on 4.4 (haven't tried on other versions) they're not. 此时,在较新版本的OS上,它确定状态是相等的,而在4.4(尚未在其他版本上尝试过)上,状态是不相等的。

Does anyone know if I'm doing something wrong here? 有人知道我在这里做错什么吗? Thank you. 谢谢。

试试这个,应该可以工作:

  if (pd.getConstantState().equals(new ColorDrawable(R.color.white).getConstantState()))

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

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