如何在单击时更改按钮的颜色,并在下次单击时恢复为默认颜色?

How to change color of button when being click,and revert back to default color in next click?

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

我检查了几个 SO 问题,但仍然没有得到我 want.So 我的解决方案如下所示,不会产生任何错误但是当点击按钮时,没有任何反应。

 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 answer,所以我修改了我的代码如下,但是当点击按钮时仍然没有任何反应。

 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();
            }
        }
    });

有人请指出我遗漏了什么,我想要的是在第一次点击时以编程方式更改按钮颜色,并在下次点击时改回默认值(避免来自同一用户的多次点赞)。

您应该创建一个选择器文件。在 drawable 文件夹中创建一个文件,例如 color_change.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
    android:drawable="@color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
    android:drawable="@color/button_focused"/> <!-- focused -->
<item android:drawable="@color/button_default"/> <!-- default -->
</selector>

并像这样在按钮中声明它

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/color_change"
    android:text="Click Me" />

而不是 clicked 或无条件,为 likedislike 操作创建和使用更新数据库的条件。因此,在点击监听器中获取以前用户喜欢或不喜欢的数据,然后根据新点击更改背景并更新数据库。

尝试将此行添加到主布局中的 row.xml 文件中:

android:descendantFocusability="blocksDescendants"

你好尝试一下希望这可以帮助你...

在XML

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

在适配器中Class

  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();
            }
        });

看看这个。在这里,我在单击时更改了按钮文本颜色。第一次,所有按钮都显示为白色,单击后它会按预期在红色和白色之间切换。 --

//LikeAdapter.java

public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeHolder> {

    public LikeAdapter() {

    }

    @Override
    public LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
        return new LikeHolder(view);
    }

    @Override
    public void onBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {

        holder.red_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (holder.red_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                } else {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                }
            }
        });

        holder.white_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.white_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                } else {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    public class LikeHolder extends RecyclerView.ViewHolder {
        private Button red_btn, white_btn;
        public LikeHolder(View itemView) {
            super(itemView);
            red_btn = (Button) itemView.findViewById(R.id.red_btn);
            white_btn = (Button) itemView.findViewById(R.id.white_btn);
            red_btn.setBackgroundColor(Color.RED);
            white_btn.setBackgroundColor(Color.WHITE);

        }
    }
}

//like_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp">



    <Button
        android:id="@+id/red_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Red"/>

    <Button
        android:id="@+id/white_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="White"/>



</RelativeLayout>