如何处理首选项(PreferenceFragment)中自定义项目上的点击事件? [英] How to handle click events on custom items in the preference(PreferenceFragment)?

查看:73
本文介绍了如何处理首选项(PreferenceFragment)中自定义项目上的点击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为首选项创建了自定义布局,以便向其中添加新的自定义项目.我使用 android:layout 属性添加该布局.我的自定义布局如下所示:

I created custom layout for preference in order to add new custom item to it. I add that layout with android:layout property. My custom layout looks like that:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:gravity="center_vertical"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:background="?android:attr/selectableItemBackground"
    android:clipToPadding="false"
    android:baselineAligned="false">

    <include layout="@layout/image_frame"/>

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingTop="16dp"
        android:paddingBottom="16dp">

        <TextView
            android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceListItem"
            android:ellipsize="marquee"/>

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_gravity="start"
            android:textAlignment="viewStart"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="10"
            style="@style/PreferenceSummaryTextStyle"/>

    </RelativeLayout>

    <ImageView
        android:id="@+id/`preference_info`"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_info"
        android:layout_marginStart="16dp"
        tools:ignore="ContentDescription" />

    <LinearLayout
        android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="end|center_vertical"
        android:paddingStart="16dp"
        android:paddingEnd="0dp"
        android:orientation="vertical"/>

</LinearLayout>

我的新商品具有 preference_info ID.如何处理 onPreferenceTreeClick 上的单击事件以处理整个首选项行上的简单单击事件?

My new item has preference_info id. How to handle click event on onPreferenceTreeClick to handle simple click events on the whole preference line?

推荐答案

从首选项中获取自定义布局视图的这种简单方法,您必须创建一个自定义首选项,以扩展 Preference androidx.preference 并覆盖

This no straight forward way to grab custom layout views from Preferences, you would have to create a custom Preference that extends Preference from androidx.preference and override either onBindView(view:View) methods, only there can you get hold of the views in your Preference's layout.

 class CustomInfoPreference(context:Context) : Preference(context){
      constructor(context: Context, attrs: AttributeSet): super(context,attrs)

      override protected fun onBindView(view:View){ 
        super(view)
      val preferenceInfo = view.findViewById<ImageView>(R.id.preference_info) 
       
       preferenceInfo.setOnClickListener{
           // Perform action!
          }
        }

     }

然后在您的preference.xml中,可以使用如下所示的自定义首选项:

Then in your preference.xml you can use the custom preference like this:

<PreferenceCategory
android:title="...." >

<com.example.appname.CustomInfoPreference
        android:key="pref key"
        android:title="your pref title"
        android:summary="your pref summary"
        android:defaultValue=""
        android:layout="@layout/custom_preference_layout" />

 </PreferenceCategory>

也请检出此SO线程: Android设置自定义首选项布局

Also checkout this SO thread: Android Set Custom Preference Layout

这篇关于如何处理首选项(PreferenceFragment)中自定义项目上的点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆