How to use setOnPreferenceChangeListener for QuietlyCoding NumberPicker?(如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?)
问题描述
我想检测 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 setOnPreferenceChangeListener 进行 QuietlyCoding NumberPicker?
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01