问题描述
我要当从值 NumberPicker
更改为检测。我对这个code我的 preferenceActivity
:
公共类主要preFS延长$ P $ {pferenceActivity @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.add preferencesFromResource(R.xml.main_ preferences);
this.find preference(SMSSentLimit)。西顿preferenceChangeListener(
新在preferenceChangeListener(){ @覆盖
公共布尔在preferenceChange(preference preference,
对象为newValue){
TrackerService.updateStats(Long.de code(newValue.toString()));
返回true;
}
});
this.find preference(NumberPickerLimit)。西顿preferenceChangeListener(
新的preference.On preferenceChangeListener(){ @覆盖
公共布尔在preferenceChange(preference preference,
对象为newValue){
Log.i(关于preferenceChange,NumberPicker更改);
Toast.makeText(getBaseContext(),CHANGEEEED !!!,
Toast.LENGTH_SHORT).show();
返回true;
}
});
}
}
第二个(找到preference(NumberPickerLimit)
)是 NumberPicker
,它是从来不叫,如果我将其更改为上preferenceClickListener
它的工作原理,但是当我点击preference,而不是当我改变了我的价值发现。
Acording源$ C $ C应该叫:
公共无效的onClick(DialogInterface对话,诠释它){
切换(这){
案例DialogInterface.BUTTON_POSITIVE:
saveValue(mPicker.getCurrent());
打破;
默认:
打破;
}
}私人无效saveValue(INT VAL){
。getEditor()putInt(getKey(),val)的.commit();
notifyChanged();
}
What's发生了什么?它是一个错误吗?
修改
这里是我的XML:
< com.michaelnovakjr.numberpicker.NumberPicker preference
机器人:键=NumberPickerLimit
机器人:标题=@字符串/ NumberPickerTitle
机器人:总结=@字符串/ NumberPickerSummary
选择器:设置defaultValue =1
选择器:startRange =1
选择器:endRange =31/>
您必须自己打电话的听众。取而代之的 notifyChanged()
,试试 callChangeListener(VAL)
因为在这里看到:
<一个href=\"https://$c$c.google.com/p/standings/source/browse/trunk/trunk/com.michaelnovakjr.numberpicker.android/src/com/michaelnovakjr/numberpicker/NumberPicker$p$pference.java?r=8\"相对=nofollow> NumberPicker preference
I want to detect when the value from the NumberPicker
is changed. I have this code on my PreferenceActivity
:
public class MainPrefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addPreferencesFromResource(R.xml.main_preferences);
this.findPreference("SMSSentLimit").setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
TrackerService.updateStats(Long.decode(newValue.toString()));
return true;
}
});
this.findPreference("NumberPickerLimit").setOnPreferenceChangeListener(
new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
Log.i("onPreferenceChange", "NumberPicker Changed");
Toast.makeText(getBaseContext(), "CHANGEEEED !!!",
Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
The second one (findPreference("NumberPickerLimit")
) is the NumberPicker
and it is never called, if I change it to onPreferenceClickListener
it works but I detect when I click the preference instead when I change the value.
Acording to source code it should be called:
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
saveValue(mPicker.getCurrent());
break;
default:
break;
}
}
private void saveValue(int val) {
getEditor().putInt(getKey(), val).commit();
notifyChanged();
}
What´s happening? Is it a bug?
EDITHere is my XML:
<com.michaelnovakjr.numberpicker.NumberPickerPreference
android:key="NumberPickerLimit"
android:title="@string/NumberPickerTitle"
android:summary="@string/NumberPickerSummary"
picker:defaultValue="1"
picker:startRange="1"
picker:endRange="31" />
You have to call the listener yourself. Instead of notifyChanged()
, try callChangeListener(val)
as seen here:
这篇关于如何使用挂线preferenceChangeListener为QuietlyCoding NumberPicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!