android - 在 Android Studio 3.1.2 中使用 Anko 从 xml 文件加载时,是否有一种简单的方法来获取控件?
问题描述
我创建了一个自定义首选项布局,并使用 UIPreference.kt 显示 UI。
我可以简单地处理按钮btnCloseimport kotlinx.android.synthetic.main.layout_preference.*.
现在我希望简单地处理 CheckBoxPreference chAutoRestore,但是布局是从 xml\mypreference.xml 加载的,我该怎么办?
目前,我必须使用代码 val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference
UIPreference.kt
import kotlinx.android.synthetic.main.layout_preference.*
class UIPreference : AppCompatPreferenceActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_preference)
fragmentManager.beginTransaction().replace(R.id.content, MyPreferenceFragment()).commit()
btnClose.setOnClickListener {
finish()
}
}
class MyPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
addPreferencesFromResource(R.xml.mypreference)
val checkboxPref = preferenceManager.findPreference(getString(R.string.IsAutoRestore)) as CheckBoxPreference
checkboxPref.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
logError( "Pref " + preference.key + " changed to " + newValue.toString())
true
}
}
}
}
layout_preference.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/ad_unit_id"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="10dp"
app:layout_constraintBottom_toTopOf="@+id/btnClose"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adView"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
<Button
android:id="@+id/btnClose"
style="@style/myTextMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:text="@string/BtnClose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
我的偏好.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="@string/PreferenceSummary"
android:title="@string/PreferenceTitle" >
<PreferenceCategory android:title="General">
<EditTextPreference
android:defaultValue="7000"
android:inputType="number"
android:key="WebServerPort"
android:summary="My PreferencePortSummary"
android:title="My Title"
/>
</PreferenceCategory>
<PreferenceCategory android:title="Auto Restore">
<CheckBoxPreference
android:id="@+id/chAutoRestore"
android:key="@string/IsAutoRestore"
android:summary="Auto restore every 15"
android:title="Auto Restore" />
</PreferenceCategory>
</PreferenceScreen>
致雷蒙德·阿特亚加
谢谢!
chAutoRestore 是 CheckBoxPreference 的 idandroid:id="@+id/chAutoRestore"
我希望我可以使用以下代码
chAutoRestore.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { preference, newValue ->
...
}
就像
btnClose 是 Button 的 id android:id="@+id/btnClose"
我可以使用代码import kotlinx.android.synthetic.main.layout_preference.*.
btnClose.setOnClickListener {
...
}
解决方案
推荐阅读
- php - 试图对一个包含特殊字符的数组进行 json_encode。json的echo为空 print_r显示sql的内容
- javascript - 如何在 JavaScript 中为数字添加小数?
- algorithm - 动态规划算法 - 花费 A 美元的方法数
- python - python在执行时关闭 pyautogui.press('enter')
- azure - 使用 gradle 错误将 Jar 发布到 Azure Artifact
- r - 您如何正确解释 gRain - “querygrain” 函数结果?潜在的纠错
- python - 无论我多么努力,Python代码都不会在新行中显示列表值
- java - 无法在 Gradle 扩展中使用自定义变量
- c++ - C++ 按索引对向量进行排序
- java - 从我们在android中的应用程序更改另一个应用程序的权限