gpt4 book ai didi

android - 复选框首选项屏幕

转载 作者:行者123 更新时间:2023-11-29 22:11:58 25 4
gpt4 key购买 nike

我正在用这个 xml 代码做一个 preferenceScreen:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:orderingFromXml="true">
<PreferenceCategory
android:key="pref1"
android:title="Search All">
<CheckBoxPreference
android:key="check_all"
android:title="Check"/>
</PreferenceCategory>
<PreferenceCategory
android:title="Specific Search"
android:key="pref2">
<ListPreference
android:title="Food"
android:key="opcion1"
android:dialogTitle="Escoge Categoria"
android:entryValues="@array/codigoCateg"
android:entries="@array/categorias"
android:negativeButtonText="Cancel"/>
<ListPreference android:title="Dificultad"/>
<ListPreference android:title="Otras"/>
</PreferenceCategory>

Java代码:

public class OpcionesBusqueda extends PreferenceActivity {

boolean CheckBoxPreference;
private PreferenceCategory Pref2;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.video_search);

Pref2=(PreferenceCategory)findPreference("pref2");

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

CheckBoxPreference = prefs.getBoolean("check_all", true);

if(CheckBoxPreference==true){
Pref2.setEnabled(false);
}
else{
Pref2.setEnabled(true);
}
}

我想做的是,如果复选框被选中,那么 pref2 (preferencecategory2) 应该被禁用,如果它没有被选中,它应该被启用。我做错了什么,或者可能不止一个。

谢谢。


我按照 Alex 的回答做了一些修改,我得到了想要的代码,这里是更正的代码:

public class OpcionesBusqueda extends PreferenceActivity {

private CheckBoxPreference Check;
private PreferenceCategory Pref2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.video_search);

Check=(CheckBoxPreference)findPreference("check_all");
Pref2=(PreferenceCategory)findPreference("pref2");

Check.setOnPreferenceChangeListener(new CheckBoxPreference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
Pref2.setEnabled((Boolean)newValue == false);
return true;
}
});

最佳答案

您可能需要为 check_all 首选项设置 OnPreferenceChangeListener 并在 onPreferenceChaged 中更改 pref2 OnPreferenceChangeListener

CheckBoxPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
boolean onPreferenceChange(Preference preference, Object newValue) {
Pref2.setEnabled((Boolean)newValue == false);
}
}

关于android - 复选框首选项屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com