【问题标题】:Get color from floating action button and use it to change background color on edittext从浮动操作按钮获取颜色并使用它来更改edittext上的背景颜色
【发布时间】:2016-12-13 16:55:42
【问题描述】:

我想知道是否有一种方法可以通过单击浮动操作按钮来更改 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 及更高版本上工作。

【问题讨论】:

标签: java android button colors background-color


【解决方案1】:
FloatingActionButton btnTag = new FloatingActionButton(R.id.some_button);
Drawable background = btnTag.getContentBackground();

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

【讨论】:

  • 这可行,但我忘了提到我需要它在 api 11 级及更高版本上工作,而这只适用于 api 16+。我会更新问题,但感谢您提供示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 2021-05-10
  • 2021-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多