简体   繁体   English

单击首选项标题Android时调用方法

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

I would like to call a method to clear cache when i click on a specific header in my preference screen in Android. 当我点击Android中偏好设置屏幕中的特定标题时,我想调用一种清除缓存的方法。

The problem is that onSharedPreferenceChanged is never called : 问题是永远不会调用onSharedPreferenceChanged:

Here is the piece of code of my preferences.xml : 这是我的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>

And, here is the code of my settingActivity : 而且,这是我的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();
    }

}

You don't need OnSharedPreferenceChangeListener in this case. 在这种情况下,您不需要OnSharedPreferenceChangeListener。 What you need is to override onHeaderClick method in your PreferenceActivity. 您需要的是覆盖PreferenceActivity中的onHeaderClick方法。

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

Of course you have to add an id to header in xml. 当然,您必须在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.

相关问题 仅在应用程序首次在Android中运行时才使用共享首选项来调用方法吗? - Use shared preference to call a method only at the time when application runs first time in Android? 当我单击手机上的首选项标题时,应用程序崩溃 - App crashes when i click on preference header on phone 通知点击的Android通话方法 - Android call method on notification click android:点击TableRow的调用方法 - android: Call method on Click of a TableRow Android在首选项片段中按下后退按钮时会调用什么方法 - Android what method is called when back button is pushed in a Preference Fragment 如何在Android ListView Header上调用click事件? - How to call click event on Android ListView Header? 如何在首选项单击事件上调用DialogFragment? - How to call a DialogFragment on Preference click event? 首选项更新时的运行方法 - Run Method when Preference is Updated Android:如何从偏好活动类中调用主类的方法? - Android: How do I call a method of the main class from the preference activity class? 在共享首选项中,当调用 editor.clear() 方法时,它只删除值或删除键值对 - In shared preference, when call editor.clear() method, It delete only values or delete key value pairs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM