AppCompatPreferenceActivity与addPreferencesFromResource不起作用

时间:2016-05-01 13:47:30

标签: android android-preferences

我正在使用AppCompatPreferenceActivity,以便我可以在屏幕顶部添加工具栏。问题是工具栏上的后退按钮不起作用。 由于我没有使用自己的布局,因此我无法访问工具栏,无法按照建议设置setNavigationOnClickListener here

这是我的活动:

public class SettingsActivity extends AppCompatPreferenceActivity {
private AppCompatDelegate mDelegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // Load the preferences from an XML resource
    super.onCreate(savedInstanceState);

        getSupportActionBar().setTitle(R.string.preferences);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();
}

/**
 * This fragment contains a second-level set of preference that you
 * can get to by tapping an item in the first preferences fragment.
 */
public static class PreferencesMain extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);


        ((Preference)findPreference(getString(R.string.settings_account_about))).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(), "About clicked", Toast.LENGTH_LONG).show();
                return true;
            }
        });

        ((Preference)findPreference(getString(R.string.settings_account_logout))).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                new UserSignOutRequest(getActivity(),
                    new ServerSuccessListener<CreateUserEntity>() {
                        @Override
                        public void onSuccessResponse(String response, CreateUserEntity jsonResponse) {
                            UserControl.signOut(getActivity());
                            Utilities.startSignInActivity(getActivity());
                        }

                    },
                    new ServerErrorListener() {
                        @Override
                        public void onErrorResponse(ServerRequestError serverRequestError) {

                        }
                        }
                ).execute(getActivity());
                return true;
            }
        });

        ((Preference)findPreference(getString(R.string.settings_account_deleteAccount))).setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                Toast.makeText(getActivity(), "Delete Account clicked", Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }

@Override
protected boolean isValidFragment (String fragmentName) {
    return true;
}



@Override
public void onBackPressed() {
    finish();   // If on first fragment then exit then activity
}

}

这是我的XML文件:

    

    <!-- This PreferenceScreen tag sends the user to a new fragment of
         preferences.  If running in a large screen, they can be embedded
         inside of the overall preferences UI. -->
    <PreferenceScreen
        android:title="@string/settings_notifications_managePushTitle"
        android:summary="@string/settings_notifications_managePushSummary"
        android:background="@android:color/black">


            <PreferenceCategory
                android:title="@string/settings_notifications_managePushHeader">

                <CheckBoxPreference
                    android:key="checkbox_preference"
                    android:title="@string/settings_notifications_managePushEntry1Title"
                    android:summary="@string/settings_notifications_managePushEntry1Summary" />
            </PreferenceCategory>

    </PreferenceScreen>

    <PreferenceScreen
        android:title="@string/settings_notifications_manageEmailTitle"
        android:summary="@string/settings_notifications_manageEmailSummary"
        android:background="@android:color/black">
            <PreferenceCategory
                android:title="@string/settings_notifications_manageEmailHeader">

                <CheckBoxPreference
                    android:key="checkbox_preference"
                    android:title="@string/settings_notifications_managePushEntry1Title"
                    android:summary="@string/settings_notifications_managePushEntry1Summary" />
            </PreferenceCategory>

    </PreferenceScreen>

    <PreferenceScreen
        android:title="@string/settings_notifications_manageSMSTitle"
        android:summary="@string/settings_notifications_manageSMSSummary"
        android:background="@android:color/black">

        <PreferenceCategory
            android:title="@string/settings_notifications_manageSMSHeader">

            <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="@string/settings_notifications_managePushEntry1Title"
                android:summary="@string/settings_notifications_managePushEntry1Summary" />
        </PreferenceCategory>

    </PreferenceScreen>


</PreferenceCategory>

<PreferenceCategory
    android:title="@string/settings_moneyAndCredits"
    android:layout="@layout/preference_category">

    <!-- The key value is unused but is required so that findPreference can work -->
    <customviews.IconPreference android:title="@string/settings_moneyAndCredits_defaultPaymentMethod"
        android:key="@string/settings_moneyAndCredits_defaultPaymentMethod"
        app:preferenceIcon="@drawable/settings_payment_method"/>
    <customviews.IconPreference android:title="@string/settings_moneyAndCredits_incomingPaymentAccounts"
        android:key="@string/settings_moneyAndCredits_incomingPaymentAccounts"
        app:preferenceIcon="@drawable/settings_manage_incomings"/>


</PreferenceCategory>


<PreferenceCategory
    android:title="@string/settings_account"
    android:layout="@layout/preference_category">
    <!-- The key value is unused but is required so that findPreference can work -->
    <customviews.IconPreference android:title="@string/settings_account_about"
        android:key="@string/settings_account_about"
        app:preferenceIcon="@drawable/settings_about"/>
    <customviews.IconPreference android:title="@string/settings_account_logout"
        android:key="@string/settings_account_logout"
        app:preferenceIcon="@drawable/settings_logout"/>
    <customviews.IconPreference  android:title="@string/settings_account_deleteAccount"
        android:key="@string/settings_account_deleteAccount"
        app:preferenceIcon="@drawable/settings_delete_user"/>

</PreferenceCategory>

我正在使用两个级别的PreferenceScreen,因为我需要混合不同的类型,而任何其他方式都给我叠加的屏幕。

1 个答案:

答案 0 :(得分:1)

我认为您可以在两种情况下访问ActionBar,即默认布局和自定义布局(就getSupportActionBar()而言,ActionBar都会返回)。您已经通过添加getSupportActionBar().setDisplayShowHomeEnabled(true);启用了后退按钮,您只需覆盖onOptionsItemSelected中的Activity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        // Write your code here
        return true;
    }
    return super.onOptionsItemSelected(item);
}

或者,如果您想在Fragment中添加此方法,只需在片段setHasOptionsMenu(true);方法中添加onCreate并覆盖onOptionsItemSelected中的Fragment即可。 我想这就是你想要的。不是吗?