0

我有一个更改动画持续时间的代码,首先启动程序“preferenze”运行以读取 sharedpreferences,稍后,用户可以通过 Preferences.class 更改动画持续时间。似乎还可以,但我无法更新首选项,我的代码:

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

// 每次应用启动时运行

preferenze();

// 读取共享私有 void preferenze() {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        CheckboxPreference = prefs.getBoolean("checkboxPref", true);
        String ListPreference = prefs.getString("listpref", "1500");


public boolean onCreateOptionsMenu(Menu menu) {

menu.add("Impostazioni").setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                    //Toast.makeText(getApplicationContext(), "Vai al Racconto... Non attivo", Toast.LENGTH_SHORT).show();
                    //goToPage();
                Intent settingsActivity = new Intent(getBaseContext(),
                        Preferences.class);
        startActivity(settingsActivity);

public void onSharedPreferenceChanged(SharedPreferences prefs,
            String listpref) {
        Toast.makeText(getApplicationContext(), "eseguo changed: "+listpref, Toast.LENGTH_SHORT).show();
          if (fade == 500){
            animazione = R.style.MyCustomTheme1;
        }
        else if (fade == 1000){
            animazione = R.style.MyCustomTheme2;
        }
        else if (fade == 1500){
            animazione = R.style.MyCustomTheme3;
        }
        else if (fade == 2000){
            animazione = R.style.MyCustomTheme4;
        }

    }

显现:

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="17" />
4

1 回答 1

0

If your onSharedPreferenceChanged is being called then you forget to commit your edited changes to your properties file. Try to replace you method with the following one:

public void onSharedPreferenceChanged(SharedPreferences prefs,
            String listpref) {
        Toast.makeText(getApplicationContext(), "eseguo changed: "+listpref, Toast.LENGTH_SHORT).show();
          if (fade == 500){
            animazione = R.style.MyCustomTheme1;
        }
        else if (fade == 1000){
            animazione = R.style.MyCustomTheme2;
        }
        else if (fade == 1500){
            animazione = R.style.MyCustomTheme3;
        }
        else if (fade == 2000){
            animazione = R.style.MyCustomTheme4;
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("listpref", fade);
        editor.commit();
    }

IDK if this your point, but you should follow the kind of ideia. After the change always commit edited values. Let me know if it works.

于 2012-12-30T12:59:17.900 回答