本文介绍了按下时更改浮动操作按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将浮动操作按钮用作某种收藏夹按钮,即书签功能.我使用的是 boolean ,初始化方式如下:

I want to use the floating action button as some sort of a favorite button, i.e. a bookmark function. I am using a boolean, initialized like so:

boolean favSelected = false;

,我的活动将从我的SQLite数据库中检索一些信息,以确定 favSelected 是true还是false.如果是真的,我希望我的晶圆厂使用不同的颜色,如果是假的,则使用原始颜色.我试过了:

and my activity will retrieve some information from my SQLite database to determine whether favSelected will be true or false. If it's true, I would want my fab to be in a different color, and if false the original color. I tried this:

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(favSelected = false) {
                favSelected = true;
                fab.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{getResources().getColor(R.color.header_pressed)}));
            } else if ( favSelected = true){
                favSelected = false;
                fab.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{getResources().getColor(R.color.colorPrimary)}));
            }

        }
    });

但是没有用.预期的功能有点像复选框

but it didn't work. The intended function is a bit like a checkbox

推荐答案

您正在使用 if(favSelected = false)这是对favSelected的赋值,请像(favSelected ==错误).

You are using if(favSelected = false) This is assignment to favSelected, Please use as if(favSelected == false).

为了进行比较,我们使用 == 符号而不是 =

For comparisons we use == sign instead of =

所以像这样修改您的代码

so Modify your code like this

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View view) {

                if (!favSelected) {
                    favSelected = true;
                    view.setBackgroundTintList(new ColorStateList(new int[][]
                            {new int[]{0}}, new int[]{getResources().getColor(R.color.colorAccent)}));
                } else if (favSelected) {
                    favSelected = false;
                    view.setBackgroundTintList(new ColorStateList(new int[][]{new int[]{0}}, new int[]{getResources().getColor(R.color.colorPrimary)}));
                }

            }
        });

这篇关于按下时更改浮动操作按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-11 02:28