汗~~~研究了一天,貌似ListPreference中根本就没有什么回调函数可以用
于是,向上一层,把注意力集中于SharedPreferences,
发现有这么个接口:onSharedPreferenceChanged
抱着试试看的态度,实现了该接口,发现此方法可行,先将部分代码分享如下:
package yinger.firewall;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
public class CallFireWall extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
ListPreference lp = null;
String whiteMode = "1";
String blackMode = "2";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
this.setTitle("Ying_er" + " Call Safe");
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
lp = (ListPreference) findPreference(getString(R.string.pref_mode_key));
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
// TODO Auto-generated method stub
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
System.out.println("onSharedPreferenceChanged");
if (key == getString(R.string.pref_mode_key)){
System.out.println(lp.getValue());
if(lp.getValue().equals(whiteMode)){
}
}
}
}
注意:该事件需要注册。既:
prefs.registerOnSharedPreferenceChangeListener(this);
posted on 2011-04-05 15:47
Ying-er 阅读(8185)
评论(0) 编辑 收藏 所属分类:
Android