簡體   English   中英

單擊首選項標題Android時調用方法

[英]Call a method when click on preference header Android

當我點擊Android中偏好設置屏幕中的特定標題時,我想調用一種清除緩存的方法。

問題是永遠不會調用onSharedPreferenceChanged:

這是我的preferences.xml的代碼片段:

<header android:icon="@drawable/ic_action_cancel"
        android:title="Vider le cache d'articles"
        android:key="clearCache"
        android:summary="Clear all datas.">
</header>

而且,這是我的settingActivity的代碼:

package com.rss.preferences;

import java.io.File;
import java.util.List;

import com.rss.R;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.TextView;


public class SettingsFragment extends PreferenceActivity  {

    SharedPreferences settings;
    public static final String KEY_CLEAR_CACHE = "clearCache";

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

        // Add a button to the header list.
        if (hasHeaders()) {
            TextView txt = new TextView(this);
            txt.setText("Version 1.0 Beta");
            txt.setGravity(Gravity.CENTER);
            setListFooter(txt);
        }

        settings = getSharedPreferences("clearCache", 0);
        settings.registerOnSharedPreferenceChangeListener(new SharedPreferences.OnSharedPreferenceChangeListener() {
          @Override
          public void onSharedPreferenceChanged (SharedPreferences sharedPreferences, String key) {
            Log.d("INFO", "popopo");
          }
        });

    }

    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preferences, target);
    }

//  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//      if (key.equals(KEY_CLEAR_CACHE)) {
//            clearApplicationData();
//        }
//    }


    public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }


    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }

}

在這種情況下,您不需要OnSharedPreferenceChangeListener。 您需要的是覆蓋PreferenceActivity中的onHeaderClick方法。

@Override
public void onHeaderClick(Header header, int position) {
    super.onHeaderClick(header, position);
    if (header.id == R.id.clear_cache) {
        clearApplicationData();
    }
}

當然,您必須在xml中為標頭添加一個id。

<header android:id="@+id/clear_cache"
        android:icon="@drawable/ic_action_cancel"
        android:title="Vider le cache d'articles"
        android:key="clearCache"
        android:summary="Clear all datas.">
</header>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM