材质切换按钮共享首选项

问题描述 投票:0回答:1

我在警报对话框中有一个 MaterialButtonToggleGroup,其中有 2 个按钮,如下所示:Toggle

我无法让共享首选项正常工作,以便用户返回对话框时仍然选择其选择。

    <com.google.android.material.button.MaterialButtonToggleGroup
       android:id="@+id/toggleButton"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:singleSelection="true"
       app:selectionRequired="true"
       android:layout_gravity="center"
       android:layout_marginBottom="50dp"
       app:checkedButton="@+id/on">
    <Button
        style="?attr/materialButtonOutlinedStyle"
        android:id="@+id/on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        app:cornerRadius="15dp"
        android:textSize="20sp"
        android:text="@string/on" />
    <Button
        style="?attr/materialButtonOutlinedStyle"
        android:id="@+id/off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:cornerRadius="15dp"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:text="@string/off" />
</com.google.android.material.button.MaterialButtonToggleGroup>    

               




view.toggleButton?.addOnButtonCheckedListener { toggleButton, checkedId, isChecked ->
            if (!isChecked) return@addOnButtonCheckedListener
               if (checkedId == R.id.on) {
                       //do something
           }

           if (checkedId == R.id.off) {
                     //do something
          }
       } 



    private fun retrieveData() {
         // what goes here?
    }   

    override fun onResume() {
        super.onResume()
        retrieveData()
    }
android kotlin dialog sharedpreferences togglebutton
1个回答
0
投票

首先,您需要声明

SharedPreferences
,初始化它,然后将其值保存在用户操作上,并在片段初始化时检索该值 (
onViewCreated
),以下是代码片段:

class FragmentB : Fragment() {
    private lateinit var sharedPreferences: SharedPreferences

    ...

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        sharedPreferences = requireActivity().getSharedPreferences("MyPrefs", Context.MODE_PRIVATE)

        val toggleButton = view.findViewById<MaterialButtonToggleGroup>(R.id.toggleButton)
        toggleButton?.addOnButtonCheckedListener { toggleButton, checkedId, isChecked ->
            if (!isChecked) return@addOnButtonCheckedListener
            if (checkedId == R.id.on) {
                // save selected option ON as true
                sharedPreferences.edit().putBoolean("option", true).apply()
            }

            if (checkedId == R.id.off) {
                // save selected option OFF as false
                sharedPreferences.edit().putBoolean("option", false).apply()
            }
        }

        retrieveData()
    }

    private fun retrieveData() {
        view?.findViewById<MaterialButtonToggleGroup>(R.id.toggleButton)?.let {
            // get saved options being default as false
            val option = sharedPreferences.getBoolean("option", false)
            if (option) {
                it.check(R.id.on)
            } else {
                it.check(R.id.off)
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.