繁体   English   中英

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

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

我开发了一个应用程序,并且在s7和其他设备上一切正常,但是我在运行4.4 kitkat的较旧版本上进行了测试,并且当该应用程序在4.4中比较按钮的颜色时,它不起作用。

这是我正在使用的代码:

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())) {

                ...

此时,在较新版本的OS上,它确定状态是相等的,而在4.4(尚未在其他版本上尝试过)上,状态是不相等的。

有人知道我在这里做错什么吗? 谢谢。

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

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

暂无
暂无

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

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