引用Android偏好标签的布局
我一直在尝试在XML文件中引用一个在首选项标签中使用的文本视图。以下是我的文件结构的简化版本:
primary.xml
<PreferenceScreen
.
.
.
<PreferenceCategory
.
.
.
<Preference
android:key="pref_key"
android:layout="@layout/custom_layout"
</Preference>
custom_layout.xml
<LinearLayout
<TextView
android:id="@+id/my_text_view"
</TextView>
</LinearLayout>
primary_fragment.kt
class PrimaryFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.primary.xml, rootKey) // renders primary.xml perfectly as well as primary.xml's preference who refers to custom_layout!
.
.
.
}
我也没有任何问题引用特定的首选项标签以将onclicklistener附加到primental_fragment.kt中。但是,
findPreference<Preference>("pref_key")?.apply {
setOnPreferenceClickListener {
Log.d("debug", "onClick working!") // this works!
true
}
}
但是,我无法在primary_fragment.kt中使用ID“ my_text_view”引用文本视图。我已经尝试从超级班级中覆盖几种方法,但在其中任何一个中都没有成功使用FindViewById(...)。我的最终目标是在用户点击偏好时获取此文本视图并将其隐藏。我已经看到了有关此问题的其他几个线程,但大多数似乎已经过时了,几乎没有解释。
I've been trying to reference a TextView inside an XML file that's used inside a Preference tag. Below is a simplified version of my file structure:
primary.xml
<PreferenceScreen
.
.
.
<PreferenceCategory
.
.
.
<Preference
android:key="pref_key"
android:layout="@layout/custom_layout"
</Preference>
custom_layout.xml
<LinearLayout
<TextView
android:id="@+id/my_text_view"
</TextView>
</LinearLayout>
primary_fragment.kt
class PrimaryFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.primary.xml, rootKey) // renders primary.xml perfectly as well as primary.xml's preference who refers to custom_layout!
.
.
.
}
I have also had no issues referencing the specific Preference tag to attach an onClickListener inside primary_fragment.kt. For example,
findPreference<Preference>("pref_key")?.apply {
setOnPreferenceClickListener {
Log.d("debug", "onClick working!") // this works!
true
}
}
However, I'm unable to reference the TextView with id "my_text_view" inside primary_fragment.kt. I've tried overriding several methods from the super class and haven't been successful with findViewById(...) in any of them. My ultimate goal is to get reference to this TextView and hide it when the user clicks on the Preference. I have seen several other threads about this issue but most seem outdated and have little explanation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类解决的解决
通过创建一个自定义重新转换
Solved by creating a CustomPreference class that extends Preference
Reference this Preference as such