单击时如何更改按钮的颜色,并在下次单击时还原为默认颜色? [英] How to change color of button when being click,and revert back to default color in next click?

查看:212
本文介绍了单击时如何更改按钮的颜色,并在下次单击时还原为默认颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的RecyclerView中有一个like按钮,当用户第一次按下like按钮时,我想要的是按钮背景色将变为red颜色,并且当同一用户按下like按钮,该按钮将变回默认颜色,即white.

I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hit the like button,the button will change back to default color which is white.

我检查了几个SO问题,但仍然没有得到我想要的东西.到目前为止,我的解决方案如下所示,没有产生任何错误,但是单击按钮时没有任何反应.

I checked for few SO question,but still havent get what I want.So far my solution is like below,doesnt produce any error but when clicked the button,nothing happen.

 likeButton =(Button) view.findViewById(R.id.likeButton);

 //here for user like the post
 holder.likeButton.setOnClickListener(new View.OnClickListener() {
            boolean clicked = true;

            @Override
            public void onClick(View v) {
                if(!clicked){
                    holder.likeButton.setBackgroundColor(Color.RED);
                    clicked = true;

                    //here i will update the database

                }else{
                    holder.likeButton.setBackgroundColor(Color.WHITE);
                    clicked = false;
                     //here i will update the database
                }


            }
        });

我也检查了这个 SO答案,所以我如下修改了我的代码,但是当按钮被按下时仍然没有任何反应点击.

I checked this SO answer too,so I modified my code as below,but still nothing happens when the button is clicked.

 holder.likeButton.setBackgroundColor(Color.WHITE);
 holder.likeButton.setOnClickListener(new View.OnClickListener() {
        ValueAnimator buttonColorAnim = null;

        @Override
        public void onClick(View v) {
            if(buttonColorAnim != null){
                buttonColorAnim.reverse();
                buttonColorAnim = null;
              //here i will update the database
            }else{
                final Button button = (Button) v;//here is the line I dont undestand
                buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.WHITE);

                buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        // set the background color
                        button.setBackgroundColor((Integer) animator.getAnimatedValue());
                    }
                  //here i will update the database
                });

                buttonColorAnim.start();
            }
        }
    });

有人请指出我所缺少的,我想要的是第一次单击时以编程方式更改按钮的颜色,然后再次单击时更改为默认值(避免多次来自同一用户).

Somebody please point out what I'm missing,what I want is change button color programmatically when being click for 1st time,and change back to default for next click(which avoid multiple like from a same user).

推荐答案

您好,希望如此可以对您有所帮助...

Hi try to this hope this can help you...

在XML中

  <Button
    android:id="@+id/btnClick"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:text="click"/>

在适配器类中

  boolean click = true;


        holder.btnClick.setTag(position);
        holder.btnClick.setId(position);
        holder.btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (click) {
                    holder.btnClick.setBackgroundColor(Color.RED);
                    click = false;
                } else {
                    holder.btnClick.setBackgroundColor(Color.WHITE);
                    click = true;
                }
                notifyDataSetChanged();
            }
        });

这篇关于单击时如何更改按钮的颜色,并在下次单击时还原为默认颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆