android - 在首选项设置中膨胀布局

标签 android preferences

我有一个自定义布局,它有一个 textview 和 ratingbar ,然后我将布局设置为我的偏好的 View ,问题是我试图在我的偏好 fragment 中膨胀 View 并设置 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>
    
    *我如何实现它的代码 fragment
    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 
              }
           }
    
    请注意,您正在将布局设置为首选项类别,这并不是您想要使用它的目的,正确的方法是子类化首选项类并赋予它您想要的行为,然后您可以直接在首选项屏幕中使用它

    关于android - 在首选项设置中膨胀布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62573526/

    相关文章:

    java - 无法让屏幕显示设置选项

    Android libgdx 首选项不起作用

    javascript - FF扩展名: saving a value in preferences and retrieving in the js file

    java - Libgdx Android - GL 线程(NullPointerException)和丢失类文件

    cocoa - 首选项窗口问题(一旦打开和关闭,将无法再次打开)

    android - 如何在android中将消息/数据从广播接收器(process1)发送到服务线程(process2)?

    java - 使用 Canvas 和 Path 围绕一个点进行 2d 旋转

    android - 如果单击复选框,我的 list 应该保存在 Xml 中如何?

    android - 使用基本适配器过滤列表

    java - JSONArray 中未命名的 JSONArray