我用android 偏好设置制作了SettingsActivity
。
在SettingsActivity
我延长SherlockFragmentActivity
,所以我决定使用 PreferenceFragment 。
我创建了一个自定义类,扩展名为PreferenceFragment
GenericPreferenceFragment
,从资源加载我的首选项xml。
首选项有2个按钮和2个复选框。
所有在我的Nexus中都有效,上次更新Android,但在使用Android 4.2的三星中,按钮似乎没有启用(它们是灰色的)!
这是一个屏幕:
为什么这样?我该如何解决这个问题?
代码:
以下是 com.xxx.yyy.SettingsActivity 的一部分:
public class SettingsActivity extends SherlockFragmentActivity implements
OnPreferenceClickListener, OnClickListener {
private final String TAG = "SETTINGS";
private ImageButton homeButton;
private ImageButton settingsButton;
private TextView activityTitle;
private ProgressDialog pDialog;
private GenericPreferenceFragment gPrefFragment;
private CheckBoxPreference cbpGCM;
private CheckBoxPreference cbpMail;
private Preference pKeywords;
private Preference pLogout;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
setupActionBar();
gPrefFragment = (GenericPreferenceFragment) getFragmentManager().findFragmentById(R.id.pref_frag_id);
FontsOverride.setDefaultFont(getApplicationContext(), "DEFAULT",
"OpenSans-Regular.ttf");
layoutInitializer();
}
// @Override
// public void onBuildHeaders(List<Header> target) {
// loadHeadersFromResource(R.xml.preference_headers, target);
// }
private void setupActionBar() {
getSupportActionBar();
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.custom_abs_layout);
}
private void layoutInitializer() {
pDialog = DialogUtil.makeDialog(SettingsActivity.this);
// --- ACTIONBAR ---
homeButton = (ImageButton) findViewById(R.id.button_menu_home);
settingsButton = (ImageButton) findViewById(R.id.button_menu_settings);
activityTitle = (TextView) findViewById(R.id.tv_activity_title);
activityTitle.setText(R.string.title_settings);
homeButton.setBackgroundResource(0);
settingsButton.setBackgroundResource(0);
homeButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_return));
cbpGCM = (CheckBoxPreference) gPrefFragment.findPreference("pref_device_notification");
cbpMail = (CheckBoxPreference) gPrefFragment.findPreference("pref_mail_notification");
pKeywords = (Preference) gPrefFragment.findPreference("pref_keyword");
pLogout = (Preference) gPrefFragment.findPreference("pref_logout");
pKeywords.setOnPreferenceClickListener(this);
pLogout.setOnPreferenceClickListener(this);
cbpMail.setOnPreferenceClickListener(this);
cbpGCM.setOnPreferenceClickListener(this);
homeButton.setOnClickListener(this);
// set button on/off
...
cbpGCM.setChecked(gcmStatus);
cbpMail.setChecked(mailStatus);
}
@Override
public boolean onPreferenceClick(Preference preference) {
String key = preference.getKey();
if (key.equalsIgnoreCase("pref_keyword")) {
finish();
startActivity(...);
} else if (key.equalsIgnoreCase("pref_logout")) {
getLogout();
}else if (key.equalsIgnoreCase("pref_device_notification")) {
boolean status = cbpGCM.isChecked();
switchGCM(status);
} else if (key.equalsIgnoreCase("pref_mail_notification")) {
boolean status = cbpMail.isChecked();
switchMail(status);
}
return false;
}
}
这是 R.layout.activity_settings :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:background="#"
android:orientation="vertical" >
<fragment
android:id="@+id/pref_frag_id"
android:name="com.xxx.yyy.GenericPreferenceFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</LinearLayout>
这是我的 com.xxx.yyy.GenericPreferenceFragment :
public class GenericPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
这是 R.xml.preferences :
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#" >
<PreferenceCategory
android:key="pref_keyword_settings"
android:title="@string/label_keyword_settings" >
<Preference
android:dependency="pref_keyword_settings"
android:icon="@drawable/ic_keyword_settings"
android:key="pref_keyword"
android:summary="@string/description_keyword_preference"
android:title="@string/title_keyword_preference" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_notification_settings"
android:title="@string/label_notification_settings" >
<CheckBoxPreference
android:defaultValue="true"
android:icon="@drawable/ic_notification_settings"
android:key="pref_device_notification"
android:summary="@string/description_device_notification"
android:title="@string/title_device_notification" />
<CheckBoxPreference
android:defaultValue="true"
android:icon="@drawable/ic_mail_settings"
android:key="pref_mail_notification"
android:summary="@string/description_mail_notification"
android:title="@string/title_mail_notification" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_logout_settings"
android:title="@string/label_logout_settings" >
<Preference
android:dependency="pref_logout_settings"
android:icon="@drawable/ic_logout"
android:key="pref_logout"
android:enabled="true"
android:summary="@string/description_logout_preference"
android:title="@string/title_logout_preference" />
</PreferenceCategory>
</PreferenceScreen>
答案 0 :(得分:0)
经过一些尝试后我得到了解决方案:我必须从 R.xml.preferences 中删除android:dependency = "pref_keyword_settings"
和android:dependency = "pref_logout_settings"
。依赖项Preferences.isEnable()
结果始终为false
!