setOnPreferenceChangeListener 不适用于自定义复选框首选项
我对 CheckBoxPreference
做了一个简单的扩展,这样我就可以拥有自己的自定义视图,并且标题左侧有一个图像图标。代码如下:
public class CustomCheckBoxPreference extends CheckBoxPreference {
private Drawable icon;
public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CustomCheckBoxPreference, 0, 0);
icon = arr.getDrawable(R.styleable.CustomCheckBoxPreference_icon);
setLayoutResource(R.layout.custom_checkbox_pref);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
ImageView prefsIcon = (ImageView) view.findViewById(R.id.prefs_icon);
prefsIcon.setImageDrawable(icon);
}
问题是,由于某种原因,我设置为任何 CustomCheckboxPreference
的 OnPreferenceChangeListener
没有效果并且没有存储。我尝试重写一些 android 方法来实现调用 super,然后打印一行以查看调用了什么。值得注意的是,callChangeListener
不会被调用。正是这个方法导致了 onPreferenceChanged
的回调。我尝试在 setChecked 内部调用 onPreferenceChanged
只是为了看看会发生什么,并且 OnPreferenceChangeListener
为空:
getOnPreferenceChangeListener().onPreferenceChange(this, checked);
这就是我设置preferencechangelistener的方式:
mTwitterPref.setChecked(!mTwitter.needsAuth());
mTwitterPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("Twitter Preference Changed!");
if ((Boolean) newValue) {
if (mTwitter.needsAuth()) {
System.out.println("We Need To Login To Twitter!");
IntentUtils.startActivityForResult(ProfileAccountsActivity.this,
TwLoginActivity.class, ACTIVITY_OAUTH);
}
} else {
showDialog(DIALOG_LOGOUT_TWITTER);
}
return false;
}
});
我有点困惑至于为什么 preferencechangelistener
无法正常工作,因为我只覆盖了 onBindView
和构造函数;我在两者中都称之为超级。有什么想法吗?
I made a simple extension of CheckBoxPreference
so that I could have my own custom view with an image icon to the left of the title. The code is below:
public class CustomCheckBoxPreference extends CheckBoxPreference {
private Drawable icon;
public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CustomCheckBoxPreference, 0, 0);
icon = arr.getDrawable(R.styleable.CustomCheckBoxPreference_icon);
setLayoutResource(R.layout.custom_checkbox_pref);
}
@Override
protected void onBindView(View view) {
super.onBindView(view);
ImageView prefsIcon = (ImageView) view.findViewById(R.id.prefs_icon);
prefsIcon.setImageDrawable(icon);
}
The problem is that for some reason the OnPreferenceChangeListener
I set to any CustomCheckboxPreference
has no effect and is not stored. I tried overriding some of the android methods for the implementation calling super and then printing a line to see what gets called. Notably callChangeListener
does not get called. It is this method that leads to the callback for onPreferenceChanged
. I tried throwing in a call to onPreferenceChanged
inside of setChecked just to see what would happen and the OnPreferenceChangeListener
is null:
getOnPreferenceChangeListener().onPreferenceChange(this, checked);
This is how I set the preferencechangelistener:
mTwitterPref.setChecked(!mTwitter.needsAuth());
mTwitterPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
System.out.println("Twitter Preference Changed!");
if ((Boolean) newValue) {
if (mTwitter.needsAuth()) {
System.out.println("We Need To Login To Twitter!");
IntentUtils.startActivityForResult(ProfileAccountsActivity.this,
TwLoginActivity.class, ACTIVITY_OAUTH);
}
} else {
showDialog(DIALOG_LOGOUT_TWITTER);
}
return false;
}
});
I am a bit confused as to why the preferencechangelistener
is not working properly as I only overwrite onBindView
and the constructor; I call super in both. Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在复选框上设置 android:focusable="false" 和 android:clickable="false":
有关此线程的更多信息:自定义 ArrayAdapter 上的可点击行
Set android:focusable="false" and android:clickable="false" on the CheckBox:
More info on this thread: Clickable rows on custom ArrayAdapter
我对自定义按钮也有同样的问题。我尝试了@jeanh 提供的解决方案,它有效。但我的按钮没有被按下,只有它周围的区域被突出显示。此外,如果你有几个按钮怎么办?显然,这个解决方案行不通。因此,我决定深入挖掘,我的解决方案如下:
xml:
Java 类:
如何使用它?简单的!
我希望它对其他人有帮助。
I had the same issue with custom button. I tried the solution provided by @jeanh and it works. But my button was not pressed, only area around it was highlighted. Moreover, what if you have a few buttons? Obviously, that this solution won't work. So, I decided to dig deeper and my solution was below:
xml:
Java class:
HOW TO USE IT? Simple!
I hope it helps someone else.
我找到了解决方案!
就我而言,我将 DialogPreference 扩展到我的自定义 Dialog 类,
我也感到困惑,因为在 PreferenceFragment 中, onPreferenceChange 从未起作用。
为了解决这个问题。我在 onDialogClosed 中调用了“super.callChangeListener”。
现在,onPreferenceChange 工作得很好!
I found solution !
In my case, I extends DialogPreference to my custom Dialog class,
I also confuse, because in PreferenceFragment, onPreferenceChange never worked.
To resolve this. I called "super.callChangeListener" in onDialogClosed.
Now, onPreferenceChange worked fine !