英文:
How do I checked the material toggle button when the application is opened?
问题
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/theme_toggle_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:selectionRequired="true"
app:singleSelection="true"
app:checkedButton="@+id/theme_light">
<com.google.android.material.button.MaterialButton
android:id="@+id/theme_light"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="12dp"
android:minWidth="0dp"
android:text="@string/light_theme"
app:icon="@drawable/ic_brightness_7_black_24dp"
app:iconPadding="4dp"
android:onClick="theme__light"
android:textColor="?attr/buttoncolor"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/theme_dark"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="12dp"
android:minWidth="0dp"
android:text="@string/dark_theme"
app:icon="@drawable/ic_brightness_4_black_24dp"
app:iconPadding="4dp"
android:onClick="theme__dark"
android:textColor="?attr/buttoncolor"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
For MainActivity;the following is in the onCreate function.
sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
checked=sharedPreferences.getInt("cek",0);
if (checked==1){
AppCompatDelegate.setDefaultNightMode(darktheme);
materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
group.check(R.id.theme_dark);
});
}
else {
AppCompatDelegate.setDefaultNightMode(defaultheme);
materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
group.check(R.id.theme_light);
});
}
public void theme__dark(View view) {
AppCompatDelegate.setDefaultNightMode(darktheme);
checked=1;
SharedCheckTheme();
}
public void theme__light(View view) {
AppCompatDelegate.setDefaultNightMode(defaultheme);
checked=2;
SharedCheckTheme();
}
public void SharedCheckTheme(){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("cek",checked);
editor.apply();
recreate();
}
英文:
I choose a theme using the toggle button.my goal is to have the theme_light toggle button selected when the app is first opened.I keep the state of the themes with SharedPreferencs because I use the recreate () function in the app.
here are my codes;
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/theme_toggle_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:selectionRequired="true"
app:singleSelection="true"
app:checkedButton="@+id/theme_light">
<com.google.android.material.button.MaterialButton
android:id="@+id/theme_light"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="12dp"
android:minWidth="0dp"
android:text="@string/light_theme"
app:icon="@drawable/ic_brightness_7_black_24dp"
app:iconPadding="4dp"
android:onClick="theme__light"
android:textColor="?attr/buttoncolor"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/theme_dark"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="8dp"
android:paddingEnd="12dp"
android:minWidth="0dp"
android:text="@string/dark_theme"
app:icon="@drawable/ic_brightness_4_black_24dp"
app:iconPadding="4dp"
android:onClick="theme__dark"
android:textColor="?attr/buttoncolor"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
For MainActivity;the following is in the onCreate function.
sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
checked=sharedPreferences.getInt("cek",0);
if (checked==1){
AppCompatDelegate.setDefaultNightMode(darktheme);
materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
group.check(R.id.theme_dark);
});
}
else {
AppCompatDelegate.setDefaultNightMode(defaultheme);
materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -> {
group.check(R.id.theme_light);
});
}
public void theme__dark(View view) {
AppCompatDelegate.setDefaultNightMode(darktheme);
checked=1;
SharedCheckTheme();
}
public void theme__light(View view) {
AppCompatDelegate.setDefaultNightMode(defaultheme);
checked=2;
SharedCheckTheme();
}
public void SharedCheckTheme(){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("cek",checked);
editor.apply();
recreate();
}
答案1
得分: 3
使用 MaterialButtonToggleGroup
上的 check(int ResId)
方法:
MaterialButtonToggleGroup m = findViewById(R.id.theme_toggle_group);
m.check(R.id.theme_dark);
英文:
Use the check(int ResId)
method on the MaterialButtonToggleGroup
:
MaterialButtonToggleGroup m = findViewById(R.id.theme_toggle_group);
m.check(R.id.theme_dark);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论