[英]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.