如何获得按钮的背景形状颜色?

拉维·帕尔玛

我有 9 个自定义圆形按钮,每个按钮在单击时打开颜色选择器,底部视图中的保存按钮可检索所有数据。

我的问题是我无法正确检索颜色。而且代码可以检索 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;

                }
            }

此代码获取视图中按钮的背景颜色。但主要问题是版本支持。而且我得到的数据是按钮的最后一个颜色值。例如:如果 int color1 = (-15210) for button 1 和 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 ;
}
音频7395

要获取 API 24 以下的背景颜色,您必须使用 ColorDrawable。可以在此处的文档和答案中找到相同的文档

问题在于保存颜色的值而不是获取它。

我希望这有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章