SwitchPreference使用

SwitchPreference使用<PreferenceScreen xmlns:android=”http://schemas.android.com/

大家好,欢迎来到IT知识分享网。

1,布局

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:summary="选择想要切换的皮肤" android:title="皮肤切换"> <SwitchPreference android:key="BuildInNightMode" android:summary="应用内换肤" android:title="夜间模式" /> <SwitchPreference android:key="AssetsNightMode" android:summary="插件式换肤" android:title="夜间模式" /> <SwitchPreference android:key="SDCardNightMode" android:summary="指定sdcard路径" android:title="夜间模式" /> </PreferenceCategory> </PreferenceScreen>

2,java

public class SettingsFragment extends PreferenceFragment { public static final String BUILD_IN_NIGHT_MODE_KEY = "BuildInNightMode"; public static final String ASSETS_NIGHT_MODE_KEY = "AssetsNightMode"; public static final String SDCARD_NIGHT_MODE_KEY = "SDCardNightMode"; private SwitchPreference mBuildInNightModePreference; private SwitchPreference mAssetsNightModePreference; private SwitchPreference mSDCardNightModePreference; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.setting_preferences); mBuildInNightModePreference = (SwitchPreference) findPreference(BUILD_IN_NIGHT_MODE_KEY); mBuildInNightModePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { mAssetsNightModePreference.setChecked(false); mSDCardNightModePreference.setChecked(false); boolean boolValue = (boolean) newValue; if (boolValue) { SkinCompatManager.getInstance().loadSkin("night", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN); } else { SkinCompatManager.getInstance().restoreDefaultTheme(); } return true; } }); mAssetsNightModePreference = (SwitchPreference) findPreference(ASSETS_NIGHT_MODE_KEY); mAssetsNightModePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { mBuildInNightModePreference.setChecked(false); mSDCardNightModePreference.setChecked(false); boolean boolValue = (boolean) newValue; if (boolValue) { SkinCompatManager.getInstance().loadSkin("night.skin", null, SkinCompatManager.SKIN_LOADER_STRATEGY_ASSETS); } else { SkinCompatManager.getInstance().restoreDefaultTheme(); } return true; } }); mSDCardNightModePreference = (SwitchPreference) findPreference(SDCARD_NIGHT_MODE_KEY); mSDCardNightModePreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { mBuildInNightModePreference.setChecked(false); mAssetsNightModePreference.setChecked(false); boolean boolValue = (boolean) newValue; if (boolValue) { SkinCompatManager.getInstance().loadSkin("night.skin", null, CustomSDCardLoader.SKIN_LOADER_STRATEGY_SDCARD); } else { SkinCompatManager.getInstance().restoreDefaultTheme(); } return true; } }); } }

运行以后

SwitchPreference使用

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/48966.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信