繁体   English   中英

如何在 android 工作室中保存选中的单选按钮的 state

[英]How to save state of checked radio button in android studio

我在单选组中使用三个单选按钮到 select 主题之一来执行主题选择(默认,浅色和深色主题)。 但是当我重新启动应用程序时,将不会选择任何人。 如何保存选中单选按钮的 state 以及当我重新启动应用程序时最后选中的单选按钮保持选中状态。 我在这里附上屏幕截图和代码,请任何人详细帮助我,因为我是 android 工作室的新手。 选择浅色主题)(选择深色主题)(重新启动应用程序后

xml

<RadioGroup
                android:id="@+id/radio_group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_gravity="center"
                android:layout_marginRight="50dp"
                android:layout_marginLeft="50dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="20dp">

                <RadioButton
                    android:id="@+id/system"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Default"
                    android:layout_weight="1"
                    android:gravity="center"
                    app:backgroundTint="@null"
                    android:button="@color/transparent"
                    android:background="@drawable/radio_btn_selector"
                    android:textColor="@drawable/radio_btn_text_color"
                    android:elevation="4dp"
                    android:padding="10dp"
                    android:textSize="13dp"
                    android:textAllCaps="true"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"/>
                <RadioButton
                    android:id="@+id/light"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Light"
                    android:layout_weight="1"
                    android:gravity="center"
                    app:backgroundTint="@null"
                    android:button="@color/transparent"
                    android:background="@drawable/radio_btn_selector"
                    android:textColor="@drawable/radio_btn_text_color"
                    android:elevation="4dp"
                    android:padding="10dp"
                    android:textSize="13dp"
                    android:textAllCaps="true"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"/>
                <RadioButton
                    android:id="@+id/dark"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Dark"
                    android:layout_weight="1"
                    android:gravity="center"
                    app:backgroundTint="@null"
                    android:button="@color/transparent"
                    android:background="@drawable/radio_btn_selector"
                    android:textColor="@drawable/radio_btn_text_color"
                    android:elevation="4dp"
                    android:padding="10dp"
                    android:textSize="13dp"
                    android:textAllCaps="true"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"/>

            </RadioGroup>

Java 代码片段

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_appearance_bottom_sheet, container, false);

       // materialButtonToggleGroup = view.findViewById(R.id.btn_group);
       radioGroup = view.findViewById(R.id.radio_group);


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch(i) {
                    case R.id.system:

                        break;
                    case R.id.light:
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        break;
                    case R.id.dark:
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        break;
                }
            }
        });

        return  view;
    }

您可以使用共享偏好。

// Storing data into SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);

// Creating an Editor object to edit(write to the file)
SharedPreferences.Editor myEdit = sharedPreferences.edit();
  
// if them is Dark set it to true and when theme is Light set it to false.
   myEdit.putBoolean("savedTheme", true);
   myEdit.apply();

// Now when app is opened check the save value in shared preference
 sharedPreferences.getBoolean("savedTheme", false);
 
 Now on restart of app check
 if it is sharedPreferences value is true then set radio button to Dark else Light.

检查此链接以获取更多信息-> https://www.geeksforgeeks.org/shared-preferences-in-android-with-examples/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM