我想检测 NumberPicker 中的值何时更改.我的 PreferenceActivity 上有这段代码:
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;
}
});
}
}
第二个 (findPreference("NumberPickerLimit")) 是 NumberPicker,如果我将其更改为 onPreferenceClickListener,它永远不会被调用它可以工作,但是当我单击首选项而不是更改值时,我会检测到.
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();
}
发生了什么事?是bug吗?
What´s happening? Is it a bug?
编辑这是我的 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" />
刚刚查看了您列出的 xml 文件 这里,看起来你有 android:key="demo.preference" 那里.而在此处的代码中,您使用的是 findPreference("NumberPickerLimit").
Just looked at your xml file listed here, looks like you have android:key="demo.preference" there. Whereas in the code here, you're using findPreference("NumberPickerLimit").
这篇关于如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
使用 GPS 获取用户的当前位置Get user#39;s current location using GPS(使用 GPS 获取用户的当前位置)
requestLocationUpdate() 抛出的 IllegalArgumentExceptionIllegalArgumentException thrown by requestLocationUpdate()(requestLocationUpdate() 抛出的 IllegalArgumentException)
LocationManager 的 getLastKnownLocation 有多可靠,多久更How reliable is LocationManager#39;s getLastKnownLocation and how often is it updated?(LocationManager 的 getLastKnownLocation 有多可靠,多久更新
如何检测位置提供者?GPS 或网络提供商How to detect Location Provider ? GPS or Network Provider(如何检测位置提供者?GPS 或网络提供商)
在应用启动期间获取当前位置Get current location during app launch(在应用启动期间获取当前位置)
locationManager.getLastKnownLocation() 返回 nulllocationManager.getLastKnownLocation() return null(locationManager.getLastKnownLocation() 返回 null)