在我的 Activity 中为应用程序设置声明CheckboxPreference
后,在“as
”行中出现val mCheckBoxPreference = findPreference("preference_a") as CheckBoxPreference
警告。应该怎样做才能使转换成功?
app_preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="preference_a"
android:defaultValue="false"
android:title="Preference A"/>
</PreferenceScreen>
片段类
import android.content.Context
import android.os.Bundle
import android.preference.CheckBoxPreference
import android.preference.Preference
import android.support.v7.preference.PreferenceFragmentCompat
import android.util.Log
class MySettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.app_preferences)
val mCheckBoxPreference = findPreference("preference_a") as CheckBoxPreference
mCheckBoxPreference.onPreferenceChangeListener = this
}
// declaring PreferenceXchangeListener
private var mPreferenceXchangeListener: PreferenceXchangeListener? = null
// declaring PreferenceXchangeListener in order to communicate with Activities
interface PreferenceXchangeListener {
fun onXchange(value:Boolean)
}
override fun onAttach(context: Context) {
super.onAttach(context)
// on attach - assign parent Activity as PreferenceXchangeListener
try
{
mPreferenceXchangeListener = context as MySettingsFragment.PreferenceXchangeListener
}
catch (e:ClassCastException) {
Log.e(TAG, "onAttach::::: PreferenceXchangeListener must be set in parent Activity")
}
}
override fun onPreferenceChange(preference: Preference, newValue:Any):Boolean {
val preferenceKey = preference.key
if (preferenceKey == "preference_a")
{
(preference as CheckBoxPreference).isChecked = newValue as Boolean
// executing parent Activity's callback with the new value
mPreferenceXchangeListener!!.onXchange(newValue)
return true
}
return false
}
companion object {
private val TAG = MySettingsFragment::class.java.simpleName
}
}
Activity 类别
class MySettingsActivity : AppCompatActivity(), MySettingsFragment.PreferenceXchangeListener {
private var mCurrentValue: Boolean? = null
override fun onCreate(savedInstanceState: Bundle?) {
val mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
mCurrentValue = mSharedPreferences.getBoolean("preference_a", false)
if (mCurrentValue as Boolean)
{
setTheme(R.style.MyDarkAppCompatTheme)
}
else
{
setTheme(R.style.MyLightAppCompatTheme)
}
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_mysettings)
val settingsFragment = MySettingsFragment()
supportFragmentManager
.beginTransaction()
.replace(R.id.settings_container, settingsFragment)
.commit()
val myActionBar = actionBar
if (myActionBar != null)
{
myActionBar.setTitle(R.string.settings)
myActionBar.setBackgroundDrawable(ColorDrawable(Color.BLACK))
}
}
override fun onXchange(value:Boolean) {
if (value !== mCurrentValue)
{
mCurrentValue = value
recreate()
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
val intent = parentActivityIntent
intent?.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
onBackPressed()
return true
}
else ->
return super.onOptionsItemSelected(item)
}
}
companion object {
private val TAG = MySettingsActivity::class.java.simpleName
}
}
最佳答案
TL; DR 您必须将导入更改为android.support.v7.preference.CheckBoxPreference
。CheckBoxPreference
有两种版本:
android.preference.CheckBoxPreference
,已添加到API级别1中,并且android.preference.Preference
作为祖先类android.support.v7.preference.CheckBoxPreference
属于支持库,并且具有android.support.v7.preference.Preference
作为祖先类您的
Fragment
是PreferenceFragmentCompat
的扩展,因此findPreference()
将返回android.support.v7.preference.Preference
。由于您无法将支持Preference
类转换为它们的不支持等效项,因此,当您尝试强制转换为不支持CheckBoxPreference
时,Android Studio会显示错误消息“此转换无法成功”。