0

我用SettingsActivityandroid首选项做了一个。

在我的SettingsActivityI 扩展SherlockFragmentActivity中,所以我决定使用PreferenceFragment

PreferenceFragment我创建了一个扩展名为的自定义类GenericPreferenceFragment,它从资源加载我的首选项 xml。

首选项是 2 个按钮和 2 个复选框。

上次 android 更新在我的 Nexus 中一切正常,但在带有 Android 4.2 的三星中,按钮似乎没有启用(它们是灰色的)!

这是一个屏幕:

android < 4.4 按钮未启用

为什么要这样做?我怎样才能解决这个问题?


代码:

这是我的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>
4

1 回答 1

0

经过一些尝试,我得到了解决方案:我必须从R.xml.preferencesandroid:dependency = "pref_keyword_settings"中删除和。依赖结果总是!android:dependency = "pref_logout_settings"Preferences.isEnable()false

于 2014-11-12T13:40:41.683 回答