わかりました、質問です。私の 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
私の主な関心事はメソッドの量ですが、巨大なメソッドも悪い習慣であることはわかっています。ここでより良い解決策は何でしょうか?
完全なソース コードはこちらから入手できます。