简体   繁体   English

如何使用 DropDownPreference 更改主题

[英]How to use DropDownPreference to change theme

I want to use a DropDownPreference for my settings page but despite looking all over the internet, there doesn't seem to be any decent tutorial on how to do this.我想在我的设置页面中使用DropDownPreference ,但是尽管浏览了整个互联网,但似乎没有任何像样的教程来说明如何做到这一点。 Does anyone know what should go in the onPreferenceChange method?有谁知道onPreferenceChange方法中的 go 应该是什么? I previously used a RadioButton but now want to use a DropDownPreference for easier implementation and maintenance.我以前使用RadioButton ,但现在想使用DropDownPreference来更轻松地实现和维护。

Activity活动

class SettingsActivity : AppCompatActivity(), FragmentSettings.PreferenceXchangeListener {
    private var mCurrentValue: Boolean = false // False is the default value

    override fun onCreate(savedInstanceState: Bundle?) {
        val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        mCurrentValue = mSharedPreferences.getBoolean("preference_dark", false)

        if (mCurrentValue) {
            setTheme(R.style.MyDarkSettingsTheme)
        } else {
            setTheme(R.style.MyLightSettingsTheme)
        }

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)


        val viewllSettingsContainer = settings_container
        val root = viewllSettingsContainer.rootView


        if (mCurrentValue) {
            root.setBackgroundColor(Color.BLACK)
        } else {
            root.setBackgroundColor(Color.WHITE)
        }

        val settingsFragment = FragmentSettings()
        supportFragmentManager
            .beginTransaction()
            .replace(R.id.settings_container, settingsFragment)
            .commit()
    }


    override fun onXchange(value:Boolean) {
        when {
            mCurrentValue != value -> {
                mCurrentValue = value
                recreate()
            }
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            android.R.id.home -> {
                val intent = parentActivityIntent
                intent?.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
                onBackPressed()
                true
            }

            else ->
                super.onOptionsItemSelected(item)
        }
    }
}

Fragment分段

class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener,
    Preference.OnPreferenceClickListener {
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        addPreferencesFromResource(R.xml.app_preferences)
    }
}

I think you're over-complicating it.我认为你过于复杂了。 There is no need to involve the fragment in listening to changes to a preference it won't be handling and passing that back to the activity.没有必要让片段参与监听它不会处理的首选项的更改并将其传递回活动。 You can register a preference change listener for all preferences in your Activity and respond accordingly there.您可以为 Activity 中的所有首选项注册首选项更改侦听器,并在那里做出相应的响应。

class SettingsActivity : AppCompatActivity(), 
    SharedPreferences.OnSharedPreferenceChangeListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        sharedPreferences.registerOnSharedPreferenceChangeListener(this)
        //...
    }

    override fun onDestroy() {
        val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
        sharedPreferences.unregisterOnSharedPreferenceChangeListener(this)
        //...
        super.onDestroy()
    }

    override fun onSharedPreferenceChanged(_: SharedPreferences, key: String) {
        when (key){
            "preference_dark" -> recreate()
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM