问题描述
我有一个Settings.kt
活动,该活动具有我的应用程序的主题更改功能。
单击主题选项后,屏幕上会弹出一个themeDialog
框,并通过开关可以更改主题,如下图所示。
现在,为了在
Settings.kt
活动和themeDialog
上反映这些变化,我在意向的帮助下刷新了Settings.kt
活动,因此该主题正在正确地变化,但是活动刷新后,themeDialog
同时关闭。因此,我需要一种在Settings.kt
活动和themeDialog
框的主题更新时保持对话框打开的方法。
这是我的代码:
Settings.kt
活动
class Settings : AppCompatActivity() {
private lateinit var settingsRecyclerView: RecyclerView
private lateinit var settingsAdapter: SettingsAdapter
private lateinit var sharedPrefForNightMode: SharedPrefForNightMode
override fun onCreate(savedInstanceState: Bundle?) {
sharedPrefForNightMode = SharedPrefForNightMode(this)
if (sharedPrefForNightMode.loadNightModeState()) {
setTheme(R.style.DarkTheme)
} else setTheme(R.style.AppTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
settingsAdapter = SettingsAdapter(this)
settingsRecyclerView = findViewById(R.id.settings_recycler_view)
settingsRecyclerView.layoutManager = linearlayoutmanager(this)
settingsRecyclerView.setHasFixedSize(true)
settingsRecyclerView.adapter = settingsAdapter
}
override fun finish() {
super.finish()
overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right)
}
override fun onBackpressed() {
super.onBackpressed()
val intent = Intent(this,Dashboard::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
finish()
}
}
SettingsAdapter.kt
-在此适配器中,我有一个函数theme(context:Context)
,该函数创建了themeDialog
,而restartSettings()
函数刷新了Settings.kt
活动:
class SettingsAdapter(private val mContext: Context) :
RecyclerView.Adapter<SettingsAdapter.SettingsViewHolder>() {
private fun theme(context: Context) {
val themeDialog = Dialog(context)
themeDialog.setContentView(R.layout.theme_popup)
themeDialog.setCancelable(false)
themeDialog.show()
val themeTitle = themeDialog.findViewById<TextView>(R.id.theme_title)
val closeTheme = themeDialog.findViewById<Button>(R.id.close_theme)
closeTheme.setonClickListener {
themeDialog.dismiss()
}
val switch = themeDialog.findViewById<Switch>(R.id.dark_mode_switch)
if(sharedPrefForNightMode.loadNightModeState()) {
switch.isChecked = true
themeTitle.text = "disable Night Mode"
}
switch.setonCheckedchangelistener { _,_ ->
if (switch.isChecked) {
sharedPrefForNightMode.setNightModeState(true)
themeTitle.text = "disable Night Mode"
restartSettings()
} else {
sharedPrefForNightMode.setNightModeState(false)
themeTitle.text = "Enable Night Mode"
restartSettings()
}
}
}
private fun restartSettings() {
mContext.startActivity(Intent(mContext,Settings::class.java))
(mContext as Activity).overridePendingTransition(R.anim.fadein,R.anim.fadeout)
mContext.finish()
mContext.overridePendingTransition(R.anim.fadein,R.anim.fadeout)
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)