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



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

检查了几个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答案,所以我修改了我的代码,如下所示,但是单击按钮时仍然没有任何反应。

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

有人请指出我缺少什么,我想要的是第一次单击时以编程方式更改按钮颜色,并在下次单击时更改回默认值(这避免了来自同一用户的多个喜欢(。

你应该创建一个选择器文件。在可绘制文件夹中创建一个文件,如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" />
<</div> div class="one_answers">

嗨,尝试这个希望这可以帮助您...

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

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

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

android:descendantFocusability="blocksDescendants"

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

/

/喜欢适配器.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>

最新更新