onPreferenceClick and OnPreferenceClickListener

前端 未结 2 609
我寻月下人不归
我寻月下人不归 2021-01-21 16:19

I\'m attempting to evaluate my preferences in my java code in order to enable/disable other options it they chose not to do other options... So far i\'m trying to only implement

2条回答
  •  遥遥无期
    2021-01-21 16:37

    first implement "OnSharedPreferenceChangeListener"

    PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener
    

    Then

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
    
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        settings.registerOnSharedPreferenceChangeListener(this);
    
    }
    

    THEN

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    
        switch (key) {
            case "pref_key_auto_delete":
                boolean notificationStatus = SP.getBoolean(key, false);
                Log.d("stat", String.valueOf(notificationStatus));
                break;
    
            case "pref_key_notification_list":
                String downloadType = SP.getString(key, "n/a");
                Log.d("stat", String.valueOf(downloadType));
                break;
        }
    

提交回复
热议问题