引用Android偏好标签的布局

发布于 2025-02-05 15:53:50 字数 1345 浏览 1 评论 0原文

我一直在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2025-02-12 15:53:50

类解决的解决

class MyCustomPreference @JvmOverloads constructor(
    context: Context,
    val attr: AttributeSet? = null,
    private val defStyleAttr: Int = 0
) : Preference(context, attr, defStyleAttr) {

    init {
        layoutResource = R.layout.my_preference_layout
    }

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        with(holder.itemView.findViewById<TextView>(R.id.my_text_view)) {
            // code here to modify TextView
        }
    }
}

通过创建一个自定义重新转换

findPreference<MyCustomPreference>(getText(R.string.my_custom_preference_key))?.apply {
    // your code here (ex: setup onClickListener)
}

Solved by creating a CustomPreference class that extends Preference

class MyCustomPreference @JvmOverloads constructor(
    context: Context,
    val attr: AttributeSet? = null,
    private val defStyleAttr: Int = 0
) : Preference(context, attr, defStyleAttr) {

    init {
        layoutResource = R.layout.my_preference_layout
    }

    override fun onBindViewHolder(holder: PreferenceViewHolder) {
        super.onBindViewHolder(holder)
        with(holder.itemView.findViewById<TextView>(R.id.my_text_view)) {
            // code here to modify TextView
        }
    }
}

Reference this Preference as such

findPreference<MyCustomPreference>(getText(R.string.my_custom_preference_key))?.apply {
    // your code here (ex: setup onClickListener)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文