问题描述
我想获得的复选框preference值。该方法适用于听preference?
和什么是在preferenceChangeListener和OnShared preferenceChangeListener?!
之间的真正区别更新为什么onShared preferenceChanged不叫?
公共类设置扩展preferenceActivity工具
OnShared preferenceChangeListener //,在preferenceChangeListener
{
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preferences); //去precated警告
}
@覆盖
公共无效onShared preferenceChanged(
共享preferences共享preferences,字符串键){//做的东西
}
@覆盖
保护无效onResume(){
super.onResume();
//获取preferenceScreen()getShared preferences()registerOnShared preferenceChangeListener(本)。
// $p$pferenceManager.getDefaultShared$p$pferences(this).registerOnShared$p$pferenceChangeListener(this);
this.getShared preferences(我的prefDB,MODE_PRIVATE)
.registerOnShared preferenceChangeListener(本);
}
@覆盖
保护无效的onPause(){
super.onPause();
//获取preferenceScreen()getShared preferences()unregisterOnShared preferenceChangeListener(本)。
// $p$pferenceManager.getDefaultShared$p$pferences(this).unregisterOnShared$p$pferenceChangeListener(this);
this.getShared preferences(我的prefDB,MODE_PRIVATE)
.unregisterOnShared preferenceChangeListener(本);
}
}
这两个之间的区别是,在preferenceChangeListener
是一个名为只是回调当其 preference变化(适用于单个键),而 OnShared preferenceChange
是一个调用时任何在preferences在共享preferences
对象更改(适用于所有键)。
那么,你的情况,你需要使用在preferenceChangeListener
您复选框preference
。
下面是一个例子:
preference ckbox preF = this.find preference(CKBOX_ preF_KEY);
ckbox pref.setOn preferenceChangeListener(新在preferenceChangeListener(){
@覆盖
公共布尔在preferenceChange(preference preference,
对象为newValue){
//做的东西
}
});
方法找到preference
如果您使用的片段是不行的,但你不使用任何所以这是好的。
I want to get value of a checkboxpreference. which method is suitable for listen to preference?
And what's real difference between OnPreferenceChangeListener and OnSharedPreferenceChangeListener ?!
UPDATEWhy onSharedPreferenceChanged not called?
public class Setting extends PreferenceActivity implements
OnSharedPreferenceChangeListener // ,OnPreferenceChangeListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);// deprecated warning
}
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {// DO Stuff
}
@Override
protected void onResume() {
super.onResume();
// getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
// PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
this.getSharedPreferences("myPrefDB", MODE_PRIVATE)
.registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
super.onPause();
// getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
// PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this);
this.getSharedPreferences("myPrefDB", MODE_PRIVATE)
.unregisterOnSharedPreferenceChangeListener(this);
}
}
The difference between these 2 is that OnPreferenceChangeListener
is a callback called only when its preference changes (applies to a single key), while OnSharedPreferenceChange
is a callback called whenever any of the preferences in that SharedPreferences
object changes (applies to all keys).
So, in your case, you need to use the OnPreferenceChangeListener
with your CheckBoxPreference
.
Here is an example:
Preference ckboxPref = this.findPreference(CKBOX_PREF_KEY);
ckboxPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
//Do stuff
}
});
The method findPreference
does not work if you use fragments, but you aren't using any so it is fine.
这篇关于什么是之间在preferenceChangeListener和OnShared preferenceChangeListener不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!