Preferences (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>

내 새 항목에 preference_infoID가 있습니다. onPreferenceTreeClick전체 환경 설정 줄에서 간단한 클릭 이벤트를 처리 하기 위해 클릭 이벤트를 처리하는 방법은 무엇입니까?

누루

이것은 Preferences에서 사용자 지정 레이아웃보기를 가져 오는 직접적인 방법이 아닙니다. androidx.preferencePreference 에서 확장 사용자 지정 Preference를 만들고 onBindView (view : View) 메서드를 재정의해야 합니다. Preference의 뷰를 보유 할 수 있습니다. 형세.

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

     }

그런 다음 preferences.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

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관