0

好的,问题是。在我的 Android 应用程序中,我有两个单独的活动用于选项和主要活动。主要活动中有一个地方,当它检查选项的变化并应用样式时。它看起来像这样:

if (prefs.getBoolean("opt_changed", true)) {
        Theme = prefs.getInt("theme", Theme);
        Font = prefs.getInt("font", Font);
        Size = prefs.getInt("size", Size);

        SetApplicableStyle(this, Theme, Font, Size);

        /** Setting opt_changed to false. */
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("opt_changed", false);
        editor.commit(); // apply changes
    }

SetApplicableStyle在这里调用的方法看起来是这样的:

public void SetApplicableStyle (DTypeActivity dTypeActivity, int Theme, int Font, int Size) {
    // Retrieving the EditText and the View as objects
    final EditText edit_text = (EditText) findViewById(R.id.editText1);
    final View main_view = (View) findViewById(R.id.mainview);

    // Setting the theme
    switch(Theme){
    case 1:
        SetThemeLight (this);
    break;
    case 2:
        SetThemeBlue (this);        
    break;
    case 3:
        SetThemeDark (this);
    break;
    }

    // Setting the font
    switch(Font){
    case 1:
        SetFontSans (this);
    break;
    case 2:
        SetFontSerif (this);        
    break;
    case 3:
        SetFontMono (this);
    break;
    }

    // Setting the size
    switch(Size){
    case 1:
        SetSizeSm (this);
    break;
    case 2:
        SetSizeMd (this);       
    break;
    case 3:
        SetSizeBg (this);
    break;
    }
}

作为Set[Something][Somewhat]方法的示例,有SetThemeLight一个:

public void SetThemeLight (DTypeActivity dTypeActivity) {
        final EditText edit_text = (EditText) findViewById(R.id.editText1);
        final View main_view = (View) findViewById(R.id.mainview);  
        main_view.setBackgroundDrawable(getResources().getDrawable(R.drawable.grey_background));
        edit_text.getBackground().setAlpha(0);
        edit_text.setTextColor(getResources().getColor(R.color.DrText));

}

我的问题涉及这个简单应用程序中使用的方法的数量。我一直在考虑减少代码量并实现该SetApplicableStyle方法。现在我在想是否可以摆脱Set[Something][Somewhat]它们并将它们的线路直接连接到SetApplicableStyle开关的外壳上。我主要关心的是方法的数量,但我知道,巨大的方法也是一种不好的做法。这里有什么更好的解决方案?

完整的源代码可在此处获得。

4

1 回答 1

1

我假设您复制了方法中的大部分代码SetThemeX。因此,我建议引入一个捕捉主题本质的类并使用它:

class MyTheme {
    public int background;
    public int alpha;
    public int color;
    public MyTheme(int background, int alpha, int color) {
        this.background = background;
        this.alpha = alpha;
        this.color = color;
    }
}

使用一种方法来设置您的主题:

public void setTheme(DTypeActivity dTypeActivity, MyTheme theme) {
    final EditText edit_text = (EditText) findViewById(R.id.editText1);
    final View main_view = (View) findViewById(R.id.mainview);
    main_view.setBackgroundDrawable(getResources().getDrawable(theme.background));
    edit_text.getBackground().setAlpha(theme.alpha);
    edit_text.setTextColor(getResources().getColor(theme.color));
}

并在存储这些主题的地方保留一张地图:

Map<Integer, MyTheme> themes = new HashMap<>();
themes.put(1, new MyTheme(R.drawable.grey_background, 0, R.color.DrText));
// put other themes

在您的SetApplicableStyle方法中,您可以简单地使用

public void SetApplicableStyle (DTypeActivity dTypeActivity, int theme, int font, int size) {
    setTheme(dTypeActivity, themes.get(theme);
    // set font and size similarly
}
于 2013-04-23T10:11:00.583 回答