从浮动操作按钮获取颜色,并使用它来更改edittext上的背景颜色

时间:2016-08-08 02:02:46

标签: java android button colors background-color

我想知道是否有办法通过单击浮动操作按钮来更改edittext对象的背景颜色。现在我不需要用于设置颜色或任何排序的代码,只需要涉及从浮动操作按钮存储颜色并将其指定给edittext对象的背景颜色的部分。

我尝试了这个链接的解决方案: How to get background color of button. On click action depends on background color

上面的链接仅适用于常规按钮,但对于浮动操作按钮,我会收到错误消息:

android.support.design.widget.ShadowDrawableWrapper cannot be cast to android.graphics.drawable.ColorDrawable

我有没有办法将Color Drawable投射到FAB来做我上面要做的事情?

这是我试过的:

FloatingActionButton btnTag = new FloatingActionButton(R.id.some_button);
ColorDrawable someColor = (ColorDrawable) btnTag.getBackground();
int colorId = someColor.getColor(); 

//Then set edittext's background color to same color as FAB with id some_button
EditText et= new EditText(R.id.some_et);
et.setBackgroundColor(colorId);

但是,由于FAB是如何绘制的,这似乎不起作用,不像常规按钮是方形而且没有圆角。所以我想知道是否有任何方法可以编辑我提供的代码以使其按预期工作?

任何帮助都将不胜感激。

编辑: 我前提到我需要它才能在api 11及更高版本上工作。

1 个答案:

答案 0 :(得分:0)

FloatingActionButton btnTag = new FloatingActionButton(R.id.some_button);
Drawable background = btnTag.getContentBackground();

EditText et= new EditText(R.id.some_et);
et.setBackground(background);