onClick() 改变按钮边框颜色

onClick() to change the button border color

在我的应用程序中,我有一个带边框的圆角按钮。现在我想做的是在单击按钮时更改边框颜色。

我已经尝试了一些与我的 post 相关的解决方案,但对我的按钮没有任何影响。

我是来寻求帮助的。

round_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <stroke android:color="#606060" android:width="2dp" />
            <solid android:color="#FFFFFF"/>
            <size android:width="30dp" android:height="30dp"/>
        </shape>
    </item>
</selector>

activity_main.xml

<Button
            android:id="@+id/push_button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/round_button"
            android:text="Ok" />

round_button.xml

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_normal"></item>
    <item android:drawable="@drawable/button_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/button_pressed" android:state_selected="true"></item>
</selector>

现在我添加了可绘制文件,但没有任何改变。

请试试这个:

Your gray rounded drawable background (gray_round_button.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <stroke android:color="@color/gray" android:width="2dp" />
            <solid android:color="@color/white"/>
            <size android:width="30dp" android:height="30dp"/>
        </shape>
    </item>
</selector>

Your green rounded drawable background (green_round_button.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <stroke android:color="@color/green" android:width="2dp" />
            <solid android:color="@color/white"/>
            <size android:width="30dp" android:height="30dp"/>
        </shape>
    </item>
</selector>

Activity:

    Activity {

     private static int borderColor = Color.GRAY;

           onCreate(){

           pushButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

              Drawable bgDrawable = getBackgroundDrawable(R.drawable. gray_round_button);

              if (borderColor == Color.GRAY) {
                  borderColor = Color.GREEN;
                  bgDrawable = getBackgroundDrawable(R.drawable. green_round_button);
                } else if (borderColor == Color.GREEN) {
                  borderColor = Color.GRAY;
                  bgDrawable = getBackgroundDrawable(R.drawable. gray_round_button);
                }

                button.setBackground(bgDrawable);
                }
            });
    }//onCreate

        public Drawable getBackgroundDrawable(int resourceId) {

        Drawable backgroundDrawable;

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            backgroundDrawable = getResources().getDrawable(resourceId, getTheme());
        } else {
            backgroundDrawable = getResources().getDrawable(resourceId);
        }

        return backgroundDrawable;
    }
  }

and xml layout:

<Button
    android:id="@+id/push_button1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textAllCaps="false"
    android:background="@drawable/gray_round_button"
    android:text="Ok" />

  • 回复您的评论需要 5 个按钮。

    当点击所有按钮时,第1、3、4个按钮的背景会发生变化 但第 2,5 个按钮的背景不会改变:


attrs.xml:

<declare-styleable name="CustomBorderButton">
    <attr name="isChangeable" format="boolean" />
</declare-styleable>

xml:

 <yourPackage.CustomBorderButton
            android:id="@+id/push_button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/gray_round_button"
            android:text="Ok"
            app:isChangeable="true" />

        <yourPackage.CustomBorderButton
            android:id="@+id/push_button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/gray_round_button"
            android:text="Ok"
            app:isChangeable="false" />

        <yourPackage.CustomBorderButton
            android:id="@+id/push_button3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/gray_round_button"
            android:text="Ok"
            app:isChangeable="true" />

        <yourPackage.CustomBorderButton
            android:id="@+id/push_button4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/gray_round_button"
            android:text="Ok"
            app:isChangeable="true" />

        <yourPackage.CustomBorderButton
            android:id="@+id/push_button5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textAllCaps="false"
            android:background="@drawable/gray_round_button"
            android:text="Ok"
            app:isChangeable="false" />

CustomBorderButton.java:

public class CustomBorderButton extends Button {

    private static int borderColor = Color.GRAY;

    public CustomBorderButton(Context context) {
        this(context, null);
    }

    public CustomBorderButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomBorderButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        boolean isChangeableColor = false;

        if (attributeSet != null) {
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attributeSet, R.styleable.CustomBorderButton, 0, 0);
        isChangeableColor = typedArray.getBoolean(R.styleable.CustomBorderButton_isChangeable, false);
        typedArray.recycle();
    }

        onClickFunction(isChangeableColor);
    }


    private void changeColors(){

      Drawable bgDrawable = getBackgroundDrawable(R.drawable. gray_round_button);

      if (borderColor == Color.GRAY) {
          borderColor = Color.GREEN;
          bgDrawable = getBackgroundDrawable(R.drawable. green_round_button);
        } else if (borderColor == Color.GREEN) {
          borderColor = Color.GRAY;
          bgDrawable = getBackgroundDrawable(R.drawable. gray_round_button);
        }

        setBackground(bgDrawable);
    }

    private void onClickFunction(boolean isChangeableColor) {

        this.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            if(isChangeableColor){
                changeColors();
            }
             }
            }
        });
    }

 public Drawable getBackgroundDrawable(int resourceId) {

    Drawable backgroundDrawable;

    if (android.os.Build.VERSION.SDK_INT >= 21) {
        backgroundDrawable = getResources().getDrawable(resourceId, getContext().getTheme());
    } else {
        backgroundDrawable = getResources().getDrawable(resourceId);
    }

    return backgroundDrawable;
}
}

  • 回复您的评论需要 7 个按钮。

    当点击所有按钮时,按钮的背景可以改变:


xml:

<yourPackage.CustomBorderButton
        android:id="@+id/push_button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

    <yourPackage.CustomBorderButton
        android:id="@+id/push_button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

    <yourPackage.CustomBorderButton
        android:id="@+id/push_button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

    <yourPackage.CustomBorderButton
        android:id="@+id/push_button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

    <yourPackage.CustomBorderButton
        android:id="@+id/push_button5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

<yourPackage.CustomBorderButton
        android:id="@+id/push_button6"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

<yourPackage.CustomBorderButton
        android:id="@+id/push_button7"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:background="@drawable/gray_round_button"
        android:text="Ok" />

CustomBorderButton.java:

    public class CustomBorderButton extends Button {

    private static HashSet<Integer> selectedDays = new HashSet<>();

    public CustomBorderButton(Context context) {
        this(context, null);
    }

    public CustomBorderButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomBorderButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

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

                int id = view.getId();
                boolean isSelected = selectedDays.contains(id);

                Drawable bgDrawable;

                if (isSelected) {
                    bgDrawable = getBackgroundDrawable(R.drawable.gray_round_button);
                    selectedDays.remove(id);
                } else {
                    bgDrawable = getBackgroundDrawable(R.drawable.green_round_button);
                    selectedDays.add(id);
                }

                setBackground(bgDrawable);
            }
        });
    }

    public Drawable getBackgroundDrawable(int resourceId) {

        Drawable backgroundDrawable;

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            backgroundDrawable = getResources().getDrawable(resourceId, getContext().getTheme());
        } else {
            backgroundDrawable = getResources().getDrawable(resourceId);
        }

        return backgroundDrawable;
    }
}