简体   繁体   中英

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.

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.

Does anyone know if I'm doing something wrong here? Thank you.

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

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

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