如何在应用程序打开时选中材料切换按钮?

huangapple go评论101阅读模式
英文:

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">

&lt;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"/&gt;

&lt;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"/&gt;

</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) -&gt; {
    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;

   &lt;com.google.android.material.button.MaterialButtonToggleGroup
    android:id=&quot;@+id/theme_toggle_group&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginBottom=&quot;8dp&quot;
    app:selectionRequired=&quot;true&quot;
    app:singleSelection=&quot;true&quot;
    app:checkedButton=&quot;@+id/theme_light&quot;&gt;

    &lt;com.google.android.material.button.MaterialButton
        android:id=&quot;@+id/theme_light&quot;
        style=&quot;?attr/materialButtonOutlinedStyle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:paddingStart=&quot;8dp&quot;
        android:paddingEnd=&quot;12dp&quot;
        android:minWidth=&quot;0dp&quot;
        android:text=&quot;@string/light_theme&quot;
        app:icon=&quot;@drawable/ic_brightness_7_black_24dp&quot;
        app:iconPadding=&quot;4dp&quot;
        android:onClick=&quot;theme__light&quot;
        android:textColor=&quot;?attr/buttoncolor&quot;/&gt;

    &lt;com.google.android.material.button.MaterialButton
        android:id=&quot;@+id/theme_dark&quot;
        style=&quot;?attr/materialButtonOutlinedStyle&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:paddingStart=&quot;8dp&quot;
        android:paddingEnd=&quot;12dp&quot;
        android:minWidth=&quot;0dp&quot;
        android:text=&quot;@string/dark_theme&quot;
        app:icon=&quot;@drawable/ic_brightness_4_black_24dp&quot;
        app:iconPadding=&quot;4dp&quot;
        android:onClick=&quot;theme__dark&quot;
        android:textColor=&quot;?attr/buttoncolor&quot;/&gt;

&lt;/com.google.android.material.button.MaterialButtonToggleGroup&gt;

For MainActivity;the following is in the onCreate function.

    sharedPreferences=this.getPreferences(Context.MODE_PRIVATE);
    checked=sharedPreferences.getInt(&quot;cek&quot;,0);

    if (checked==1){
    AppCompatDelegate.setDefaultNightMode(darktheme);

        materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -&gt; {
            group.check(R.id.theme_dark);
        });
    }
    else {
        AppCompatDelegate.setDefaultNightMode(defaultheme);
        materialButtonToggleGroup.addOnButtonCheckedListener((group, checkedId, isChecked) -&gt; {
            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(&quot;cek&quot;,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);

huangapple
  • 本文由 发表于 2020年4月4日 10:35:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61023119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定