如何处理首选项PreferenceFragment中自定义项目的点击事件?

如何解决如何处理首选项PreferenceFragment中自定义项目的点击事件?

我根据偏好创建了自定义布局,以便向其中添加新的自定义项目。 我使用 android:layout 属性添加该布局。我的自定义布局如下所示:

<?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>

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

解决方法

这不是从 Preferences 获取自定义布局视图的直接方法,您必须创建一个自定义 Preference,从 androidx.preference 扩展 Preference 并覆盖 onBindView(view:View)方法,只有在那里你才能掌握你的偏好布局中的视图。

 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!
          }
        }

     }

然后在您的首选项.xml中,您可以像这样使用自定义首选项:

<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 Set Custom Preference Layout

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?