在首选项设置中膨胀布局

Inflating a layout in preference settings

我有一个自定义布局,它有一个 textview 和 ratingbar,然后我将布局设置为我的偏好视图,问题是我试图在我的偏好片段中膨胀视图并设置 ratingbar onclick,我放个日志看看有没有被点击但是没有任何反应

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Preference
        android:title="@string/YourPreferredLanguage"
        android:icon="@drawable/ic_baseline_language_24" />

    <CheckBoxPreference
        android:title="@string/french"
        android:key="french"
        />
   
    <CheckBoxPreference
        android:title="@string/portuguese"
        android:key="portuguese"
        />

    <Preference
        android:title="Rate Our App"
        android:icon="@drawable/ic_baseline_stars_24"
        />

    <PreferenceCategory
        android:layout="@layout/ratinglayout"/> //this is my custom layout

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/ratingtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="20dp"
        android:text="Rate "
        android:textColor="@android:color/black"
        android:textSize="16sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:numStars="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.75"
        app:layout_constraintStart_toEndOf="@+id/ratingtext"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

*我如何实现它的代码片段

class SettingsFragment : PreferenceFragmentCompat() {
        override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
             setPreferencesFromResource(R.xml.settingspreferences,rootKey)

val view = LayoutInflater.from(requireContext()).inflate(R.layout.ratinglayout,null,false)
            val ratingbars = view.findViewById<RatingBar>(R.id.ratingBar)
            ratingbars.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
                Log.d("TAG","Rating is " + rating) // when i click on rating bar , nothing happens
}
}

任何帮助将不胜感激,谢谢。

除了我上面的评论,如果您只想访问该首选项。

像下面那样做

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
         setPreferencesFromResource(R.xml.settingspreferences,rootKey)
         getPreferenceScreen()
         .getPreference(4)
        .setOnPreferenceClickListener { preference - > // handle click 
          }
       }

请注意,您正在将布局设置为偏好类别,这并不是您想要使用它的目的,正确的方法是子class 偏好 class 并赋予它你想要的行为,那么你可以直接在首选项屏幕中使用它