0

ユーザーsharedPreferencesが「保存」ボタンを押すたびに使用するテーマの選択を保存したいのですが、明らかに間違っていることがありますが、表示されません。

私の問題は、sharedPreferences私のアプリケーションがテーマを希望どおりに変更し、問題がないことです。しかし、sharedPreferences物事を適用すると、テーマの変更が停止し、どちらも保存されません。

何をどこで達成しようとしているのかを理解するのに役立つコメントをいくつか付けました。

これが私の設定クラスです:

public class SettingsActivity extends Activity implements OnClickListener {

public static final String PREF_NAME = "MyPrefsFile";
public static int newTheme;
public final static int THEME_DARK = R.style.DarkTheme;
public final static int THEME_LIGHT = R.style.LightTheme;
public final static int THEME_COLORS = R.style.ColorsTheme;


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Here is where I'm supposed to check the sharedPreferences then
    // Set the theme option newTheme with the users last choice, 
            // and if there is
    // a choice set the theme.
    SharedPreferences settings = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    newTheme = settings.getInt("themeCustom", 0);   

     if(newTheme == THEME_DARK) {
        setTheme(R.style.DarkTheme);

    } else if(newTheme == THEME_LIGHT){
        setTheme(R.style.LightTheme);
    } else if(newTheme == THEME_COLORS) {
        setTheme(R.style.ColorsTheme);
    } else { 
    // Utils.onActivityCreateSetTheme(this);
        setTheme(R.style.AppTheme);
     }
    setContentView(R.layout.activity_settings);

    findViewById(R.id.button2).setOnClickListener(this);
    findViewById(R.id.button3).setOnClickListener(this);
    findViewById(R.id.button4).setOnClickListener(this);
    findViewById(R.id.button5).setOnClickListener(this);
    findViewById(R.id.button6).setOnClickListener(this);
}   
@Override
public void onClick(View v) {
    Intent intent = getIntent();
    Intent main = new Intent(SettingsActivity.this,MainActivity.class);
    switch (v.getId()) {
    case R.id.button2:
        newTheme = THEME_DARK;
        finish();
        startActivity(intent);
        break;

    case R.id.button3:
        newTheme = THEME_LIGHT;
        finish();
        startActivity(intent);
        break;
    case R.id.button5:
        newTheme = THEME_COLORS;
        finish();
        startActivity(intent);
        break;

    case R.id.button4:
        // This button returns to the main activity without saving.
        startActivity(main);
        break;
    case R.id.button6:
        // this is the button save
        SharedPreferences settings = 
                               getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        SharedPreferences.Editor edit;
        edit = settings.edit();
        edit.clear();
        edit.putInt("themeCustom", newTheme);
        edit.commit();
        startActivity(main);
        break;

    default:
        break;
        }

    }

    }
4

4 に答える 4

1

変更を保存していないと思います。テーマの変更ボタンをクリックするたびに、新しいテーマの値をnewThemeタグに割り当ててからfinish()、アクティビティを実行するだけです。しかし、アクティビティを終了する前に、どこに変更を保存していますか?

アクティビティを終了する前に新しい変更をコミットする必要があります。そうすれば、変更が反映されると思います。

于 2013-05-01T05:52:00.240 に答える
1

これは、1 つの SharedPreferencec.Editor トランザクションで clear と putInt を呼び出すためだと思います。 http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()

clear を呼び出さず、putInt のみを呼び出す

于 2013-05-01T05:45:20.417 に答える
0

問題は int と long にあります。

ここで、newTheme は int で、THEME_DARK または THEME_ILGHT などを格納します。ただし、THEME_DARK およびその他の変数は長い値を持っています。つまり、R.style.DarkTheme は長い値または 16 進数の値にすることができます。

したがって、最終的にこれらの newTheme を sharedPref に保存すると、実際にはここに 0 しか保存されません。

Shared Pref に書き込む前に、newTheme 変数の値を確認できます。これで問題は解決します。

これを解決するには、これらのテーマに他の最終的な整数値を使用して、共有設定に保存できるようにします。

これで問題が解決することを願っています。

于 2013-05-01T05:51:33.207 に答える
0

共有設定を保存するには、次のコードを使用できます。

SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = prefs.edit();
prefEditor.putInt(“themeCustom”, 0); 
prefEditor.commit();
于 2013-05-01T08:47:55.800 に答える