如何获得按钮的背景形状颜色?
How to get Background Shape color of buttons?
我有 9 个自定义圆形按钮,每个按钮在单击时都会打开颜色选择器,底部视图中的保存按钮会检索所有数据。
我的问题是我无法检索颜色 correctly.and 而且代码可以检索 24 版本以上的数据。这也没有用。
示例代码
每次单击按钮时都会调用此代码。它从颜色选择器获取颜色值。
GradientDrawable c1, c2;
Button mcbt_1,mcbt_2;
// this function gets call everytime button is clicked ,it sets the color to the background and maintain the shape of button to circle.
public void onColorSelected(int color) {
switch (buttonSelected) {
case 1:
c1 = (GradientDrawable) Mcbt_1.getBackground();
c1.setColor(color);
break;
case 2:
c2 = (GradientDrawable) Mcbt_2.getBackground();
c2.setColor(color);
break;
}
}
此代码获取 view.But 中存在的按钮的背景颜色,主要问题是版本支持。我得到的数据也是按钮的最后一个颜色值。例如:如果按钮 1 的 int color1 = (-15210) 和 int color2 =(-15700) 那么当我使用这个
获取值时
c1 = (GradientDrawable) Mcbt_1.getBackground();
color_BT_1 = c1.getColor().getDefaultColor();
从上面的代码中,我得到 color_BT_1 为 -15700 和 color_BT_2 为 -15700。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
c1 = (GradientDrawable) Mcbt_1.getBackground();
color_BT_1 = c1.getColor().getDefaultColor();
c2 = (GradientDrawable) Mcbt_2.getBackground();
color_BT_2 = c2.getColor().getDefaultColor();
String edColor = color_BT_1 + "," + color_BT_2 ;
}
要获取 API24 以下的背景颜色,您必须使用 ColorDrawable。同样的文档也可以在 here docmentation and in 答案中找到。
问题在于保存颜色值而不是获取颜色值。
希望对您有所帮助。
我有 9 个自定义圆形按钮,每个按钮在单击时都会打开颜色选择器,底部视图中的保存按钮会检索所有数据。
我的问题是我无法检索颜色 correctly.and 而且代码可以检索 24 版本以上的数据。这也没有用。
每次单击按钮时都会调用此代码。它从颜色选择器获取颜色值。
GradientDrawable c1, c2;
Button mcbt_1,mcbt_2;
// this function gets call everytime button is clicked ,it sets the color to the background and maintain the shape of button to circle.
public void onColorSelected(int color) {
switch (buttonSelected) {
case 1:
c1 = (GradientDrawable) Mcbt_1.getBackground();
c1.setColor(color);
break;
case 2:
c2 = (GradientDrawable) Mcbt_2.getBackground();
c2.setColor(color);
break;
}
}
此代码获取 view.But 中存在的按钮的背景颜色,主要问题是版本支持。我得到的数据也是按钮的最后一个颜色值。例如:如果按钮 1 的 int color1 = (-15210) 和 int color2 =(-15700) 那么当我使用这个
获取值时 c1 = (GradientDrawable) Mcbt_1.getBackground();
color_BT_1 = c1.getColor().getDefaultColor();
从上面的代码中,我得到 color_BT_1 为 -15700 和 color_BT_2 为 -15700。
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
c1 = (GradientDrawable) Mcbt_1.getBackground();
color_BT_1 = c1.getColor().getDefaultColor();
c2 = (GradientDrawable) Mcbt_2.getBackground();
color_BT_2 = c2.getColor().getDefaultColor();
String edColor = color_BT_1 + "," + color_BT_2 ;
}
要获取 API24 以下的背景颜色,您必须使用 ColorDrawable。同样的文档也可以在 here docmentation and in
问题在于保存颜色值而不是获取颜色值。
希望对您有所帮助。