获取背景按钮并将此颜色应用于另一个按钮

Get background button and apply this color to another one

我在 android studio 上有一个项目,我需要获取按钮的背景颜色以将颜色应用到另一个按钮。

我试过了:

ColorDrawable BgColor = (ColorDrawable) btn_next.getBackground();
btn_filRouge.setBackgroundColor(BgColor);

我想将 ColorDrawable 转换为 int。或者直接把颜色取成int。

I want to convert ColorDrawable to int. Or to get the color into int directly.

你不能将ColorDrawable转成int,但你可以按照第二种解决方法,答案在你的问题中,你可以使用getColor() :

ColorDrawable bgColor = (ColorDrawable) btn_next.getBackground();
int color = bgColor.getColor();

有关详细信息,请查看 ColorDrawable

的文档