android - 如何从按钮中获取颜色?
问题描述
我需要在 Activity 上找到红色按钮并为这些按钮设置橙色背景颜色。
当我单击按钮时,我将其设置为红色:
view.setBackgroundTintList(ColorStateList.valueOf(Color.RED));
当我单击另一个按钮时,红色按钮应变为橙色。
public void Active(View view){
for (View but : buttons) {
but.setClickable(true);
but.setBackgroundTintList();
}
}
我不知道如何获取颜色的 id
解决方案
对我来说不清楚你的问题,但我会尽力回答。
假设按钮是 a List<Button>
,那么您可以做的是。
for(View but : buttons){
int color = ((ColorDrawable)but.getBackground()).getColor();
if(color == Color.Red){
//This button is red change it to orange
but.setBackgroundColor(R.colors.orange);
}
}
当您单击按钮时,请使用
button.setBackgroundResource(Color.Red);
推荐阅读
- email - 即使定义了 SPF 记录,也始终获得无(无 SPF 记录)
- rest - RestBase wiki 如何处理缓存
- sql-server - azure数据仓库中如何实现string_agg功能
- arrays - 使用 VBA 从字母表中第 n 位查找单词链可能性的算法
- salesforce - 有没有办法在导航栏中为自定义闪电组件打开新选项卡,记录名称为选项卡标签?
- awk - 试图在 awk 中打印上一行,但似乎打印当前行两次
- php - 根据 id 和 parent_id 属性对集合(或对象数组)进行排序
- angular - 如何在 Angular 7 应用程序中将 GWT 应用程序用作组件?
- python - 如何将顶部输出写入python中的文件?
- python - 如何在回调中替换 Kivy 小部件?