如何收听listPreference,改变屏幕方向?

时间:2016-07-31 14:11:53

标签: sharedpreferences screen-orientation preferenceactivity listpreference

我有一个PreferenceActivity,我希望可以选择更改/锁定屏幕方向。

方向应锁定主活动屏幕方向

我不确定是否以及如何调用更改侦听器。 在主要活动的onCreate和onResume上似乎是正确的,但我无法让它工作。

的settings.xml

   <ListPreference     android:title="Screen orientation"
    android:summary="current: %s"
    android:key="rotation"
    android:defaultValue="1"
    android:entries="@array/listArray"
    android:entryValues="@array/listValues" />

array.xml

<resources>
<string-array name="listArray">
    <item>Auto rotate</item>
    <item>Portrait</item>
    <item>Landscape</item>
</string-array>

<string-array name="listValues">
    <item>1</item>
    <item>2</item>
    <item>3</item>
</string-array>

偏好活动

public class MyPreferencesActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}

public static class MyPreferenceFragment extends PreferenceFragment  {

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }

}

链接我已经看过了

How to create a Listener to Preferences changes in Preferences activity?

Adding listeners to individual ListPreference items

How to listen for preference changes within a PreferenceFragment?

OnSharedPreferenceChangeListener not registering change in preference

http://envyandroid.com/android-detect-preference-changes/

0 个答案:

没有答案