Reset Android Button to Default Color And Style

2019-09-13 21:02发布

In my app I need to be able to change the background color of a button and back to the default color. Changing the color to a custom color works, but my code for reversing the process has given me issues.

My button code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_practice, container, false);
        mNomButton = (Button) view.findViewById(R.id.custom_practice_nom_button);
        mNomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mNomIsSelected = !mNomIsSelected;
                mNomButton.setBackgroundResource(mNomIsSelected ? R.color.buttonSelected : android.R.drawable.btn_default);
                updateView(mNomButton);
            }
        });
        return view;
    }

When I reset the button resource I end up with a bordered button where I had borderless before:

On layout inflate:

enter image description here

OnClick the first time:

enter image description here

OnClick the second time:

enter image description here

I would like to avoid having to create a custom drawable that mimics the flat button. Is there a way to get the default borderless button resource?

标签: android
2条回答
三岁会撩人
2楼-- · 2019-09-13 21:35

when u set background in second time set it by getting background from another button which has the default background so for ex let's say button in red is b1 and button in default is b2

Then code to set background for b1 to become default is

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        b1.setBackground(b2.getBackground());
else b1.setBackgroundDrawable(b2.getBackground());
查看更多
聊天终结者
3楼-- · 2019-09-13 21:43

It took some time because many of the answers included deprecated code, but I have a solution to my issue. Using the suggestion of Mohamed I grabbed the default Drawable value of one of the buttons and stored it. The biggest issue was finding a way for both the default color and my color from colors.xml to be the same type for my ternary if to work.

private Drawable mDefaultButtonColor;
private Drawable mSelectedButtonColor;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        mNomButton = (Button) view.findViewById(R.id.custom_practice_nom_button);

        mNomButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mNomIsSelected = toggleButton(mNomButton, mNomIsSelected);
            }
        }

        mDefaultButtonColor = ((Drawable) mNomButton.getBackground());
        mSelectedButtonColor = ContextCompat.getDrawable(getActivity(), R.color.buttonSelected);
        return view;
    }


    private boolean toggleButton(Button button, boolean isSelected) {
        isSelected = !isSelected;
        button.setBackground(isSelected ? mSelectedButtonColor : mDefaultButtonColor);
        return isSelected;
    }
查看更多
登录 后发表回答