如何手动处理复选框状态?
我需要手动控制 CheckBoxPreference
;我必须检查自己数据中的条件以确定是否可以设置首选项。
我该怎么做呢?我当前的代码如下,但似乎不起作用。
CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (something) {
return true; // checkbox should be checked
} else {
return false; // checkbox should be unchecked
}
我应该总是返回 false
然后使用
buyPref.setChecked(true);
I need to control a CheckBoxPreference
manually; I have to check a condition in my own data to determine if the preference can be set or not.
How can I do it? My current code is as follows, but it doesn't seem to work.
CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (something) {
return true; // checkbox should be checked
} else {
return false; // checkbox should be unchecked
}
Should I always return false
and then use
buyPref.setChecked(true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果处理了点击,您应该始终返回 true。来自 API :
自: API 级别 1
单击首选项时调用。
参数
首选项
- 单击的首选项。返回
True
- 如果点击已处理。因此,您的代码应为:
首选项管理器应处理是否选中该项目,但如果您想自己执行此操作:
You should always return true if the click was handled. From the API:
Since: API Level 1
Called when a Preference has been clicked.
Parameters
preference
- The Preference that was clicked.Returns
True
- if the click was handled.So your code should read:
The preference manager should handle whether the item is checked or not, but if you want to do it yourself:
我认为你想要这样的东西:
你希望始终返回 false,这样 Android 就不会尝试编写首选项本身。请参阅
OnPreferenceChangeListener
的文档更多信息。请注意,所有这些都将发生在 UI 线程上,因此如果您需要执行任何长时间运行的操作,我会将其放入
AsyncTask
带有ProgressDialog
这样用户就不会感到沮丧。I think you want something like this:
You want to always return false from this so that Android doesn't attempt to write the preference itself. See the documentation for
OnPreferenceChangeListener
for more info.Note that all of this will happen on the UI thread, so if you need to do anything long-running, I would throw it in an
AsyncTask
with aProgressDialog
so that the user doesn't get frustrated.您可以使用它来检查复选框是否被选中
设置复选框,
有帮助!
干杯
尼辛
You can use this to check if the checkboxes are checked or not
To set a checkbox,
Hope it helps!
Cheers
Nithin