【问题标题】:How to change color of button when being click,and revert back to default color in next click?单击时如何更改按钮的颜色,并在下次单击时恢复为默认颜色?
【发布时间】:2017-09-05 09:33:01
【问题描述】:

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

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

【问题讨论】:

  • 您的第一个示例似乎是正确的,但是,您的 clicked 变量已分配值 true,这意味着 !clicked 始终为 false,并且您的代码永远不会执行。
  • 所以我应该为第一个示例分配 false 吗? @MatusMak
  • 投反对票的原因?
  • 这不是我提出的,我不是因为它不鼓励人们提出问题而对问题投反对票,但我猜有人不喜欢你写问题的方式。

标签: android android-layout android-widget


【解决方案1】:

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

【讨论】:

  • 非常适合我,但我使用了彩色图像。谢谢
【解决方案2】:

你好试试这个希望这可以帮助你...

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      android:descendantFocusability="blocksDescendants"
      

      【讨论】:

      • 这个是干什么用的??
      • 要使按钮可点击,它只是将焦点带到按钮而不是您的列表。你试过了吗!
      【解决方案5】:

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

      //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>
      

      【讨论】:

        猜你喜欢
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 2021-10-04
        • 2020-02-27
        • 2021-07-07
        • 2016-02-04
        • 2021-07-21
        • 2019-05-20
        相关资源
        最近更新 更多