如何在安卓工作室中保存选中单选按钮的状态



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

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/

相关内容

  • 没有找到相关文章

最新更新