2013-03-18 56 views
1

我使用的是sharedpreferences框架,但我在與其中一個首選項進行交互時遇到了一些麻煩。我想啓用我的一個選項來重置我的應用程序。要做到這一點,我想要一個對話框出現在屏幕上的確定和取消。如果用戶點擊確定,我想要做一堆東西。使用偏好設置的Android sharedPreferences像按鈕一樣工作

我可以在使用ImageView偵聽器的正常活動中編寫所有這些內容。但在SharedPreferences框架中,我不知道如何添加偵聽器和操作。我猜想作爲額外的獎勵,我還需要與當前的SharedPreferences狀態進行交互?

此刻我有這樣的XML

<PreferenceCategory android:title="Reset App"> 

    <Preference 
     android:title="Reset App?" 
     android:summary="Click here to reset the App to defaults." 
     android:key="resetapp" /> 

</PreferenceCategory> 


<PreferenceCategory android:title="General Settings"> 
<CheckBoxPreference 
     android:title="Enable Sounds?" 
     android:defaultValue="true" 
     android:summary="A Tick here will enable sounds throughout body-mix-ology." 
     android:key="enablesounds" /> 
<CheckBoxPreference 
     android:title="High performance Phone?" 
     android:defaultValue="false" 
     android:summary="If you have a high performance phone tick here to speed up body switching." 
     android:key="highperformancephone" /> 
    </PreferenceCategory> 

,這是我的類文件

public class Preferences extends PreferenceActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.layout.preferences); 

     //TODO 
     // act on resetapp 
     // prompt onscreen confirmation. 

    } 

    public AlertDialog createDialog() { 

     return new AlertDialog.Builder(this) 
     .setTitle("Reset App?") 
     .setMessage("Are you sure? You will wipe all data from the app.") 
     .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       Toast.makeText(getBaseContext(), "App has been reset!", Toast.LENGTH_SHORT).show(); 
       //TODO 
       // clear DB 
       // reset sharedprefs. 
      } 
     }) 
     .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

       Toast.makeText(getBaseContext(), "App was not reset", Toast.LENGTH_SHORT).show(); 

      } 
     }) 
     .show(); 

    } 


} 

回答

1

如何具有與ListPreference「你確定要這麼做嗎?」並使用默認的「否」列出值「是」和「否」。然後到任何像這樣的 「是」 確認迴應:

public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { 
    if (key.equals("resetapp") && prefs.getString(key,"").equals("Yes")) { 
     // reset the value to "No" so that next time a preference change will be triggered 
     prefs.edit().putString(key, "No").commit(); 

     // now do your awesome reset stuff ... 
     Preference someOtherPref = findPreference(otherKey); 
     ... 
    } 
} 

不要忘了你的類定義更改爲:

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { 

並註冊事件偵聽:

@Override 
protected void onResume() { 
    super.onResume(); 
    getPreferenceScreen().getSharedPreferences() 
      .registerOnSharedPreferenceChangeListener(this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    getPreferenceScreen().getSharedPreferences() 
      .unregisterOnSharedPreferenceChangeListener(this); 
}