首页 > 解决方案 > 有没有办法压缩重复的颜色设置?

问题描述

如您所知,我们可以使用int a,b,c=3;而不是:

int a;
int b;
int c=3;

我使用了很多代码,如下所示。例如更改视图或其他设置。它浪费了我很多时间。有没有办法让它变得更容易?

    //<editor-fold desc="Not Dark Mode">
    Button Save=findViewById(R.id.Setting_Save_BTN);
    Button Back=findViewById(R.id.Setting_Back_BTN);
    Button Instruction=findViewById(R.id.Setting_Instruction_BTN);
    TextView Title=findViewById(R.id.Setting_Title_TXT);
    TextView Display=findViewById(R.id.Setting_Display_TXT);
    TextView Progress=findViewById(R.id.Setting_Progress_Bar_TXT);
    TextView Relaps=findViewById(R.id.Setting_Relaps_TXT);
    TextView Lang=findViewById(R.id.Setting_Language_TXT);

    findViewById(R.id.Setting_Layout).setBackgroundColor(Color.WHITE);
    Phone_Language.setTextColor(Color.BLACK);
    Persian.setTextColor(Color.BLACK);
    Arabic.setTextColor(Color.BLACK);
    English.setTextColor(Color.BLACK);
    Spanish.setTextColor(Color.BLACK);
    Hindi.setTextColor(Color.BLACK);
    Malay.setTextColor(Color.BLACK);
    Portuguese.setTextColor(Color.BLACK);
    Russian.setTextColor(Color.BLACK);
    Chinese.setTextColor(Color.BLACK);
    .
    .
    .
    OnePhase.setTextColor(Color.BLACK);
    ThreePhase.setTextColor(Color.BLACK);
    Save.setTextColor(Color.BLACK);
    Back.setTextColor(Color.BLACK);
    Relaps.setTextColor(Color.BLACK);
    Lang.setTextColor(Color.BLACK);
    //</editor-fold>

如果你能回答我,我将不胜感激...

标签: javaandroid

解决方案


您可以使用循环删除重复。

TextView[] makeBlack = new TextView [] {
    Persian, Arabic, English, Spanish,
    Hindi, Malay // add the rest
};
for (TextView view: makeBlack) {
    view.setTextColor(Color.BLACK);
}

推荐阅读