0

わかりました、質問です。私の Android アプリには、オプション用とメイン アクティビティ用の 2 つの別々のアクティビティがあります。オプションの変更をチェックしてスタイルを適用するとき、メインアクティビティに場所があります。次のようになります。

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;
    }
}

テーマを設定する方法を 1 つ作成します。

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 に答える