Java 在Android Studio设置中切换首选项后应用程序冻结
我在Android Studio中设置活动,而不是手动设置。我想做一个在应用程序中应用暗模式的开关。问题是,当我点击开关时,没有移动的动画,只有应用程序的闪烁。当我回到我的主要活动时,我看到应用了主题(我之前只尝试过主要活动),但当我尝试回到设置时,一切都冻结了!没有任何东西可以点击,没有任何反应。 这是我的Java代码:Java 在Android Studio设置中切换首选项后应用程序冻结,java,android,xml,preference,android-dark-theme,Java,Android,Xml,Preference,Android Dark Theme,我在Android Studio中设置活动,而不是手动设置。我想做一个在应用程序中应用暗模式的开关。问题是,当我点击开关时,没有移动的动画,只有应用程序的闪烁。当我回到我的主要活动时,我看到应用了主题(我之前只尝试过主要活动),但当我尝试回到设置时,一切都冻结了!没有任何东西可以点击,没有任何反应。 这是我的Java代码: import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivit
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.SwitchPreference;
public class SettingsActivity extends AppCompatActivity {
private SwitchPreference darkModeSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
SwitchPreference darkModeSwitch = (SwitchPreference) findPreference("darkmode");
assert darkModeSwitch != null;
darkModeSwitch.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
return false;
}
});
}
}
}
这是root_preferences.xml:
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory
app:title="General">
<SwitchPreference
app:key="darkmode"
app:title="Dark mode"/>
</PreferenceCategory>
我想这可能是因为我在应用主题后没有再次开始活动,因为当我在main_活动中尝试黑暗模式时,有两行,在我的设置活动中无法放置,因为类是静态的。。。请帮忙
public void onClick(View v) {
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
//These two
finish();
startActivity(new Intent(MainActivity.this, MainActivity.this.getClass()));
}
});
将其放入您的
首选项.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:defaultValue="false"
app:key="@string/key_night_mode"
app:summaryOff="@string/summary_night_mode_off"
app:summaryOn="@string/summary_night_mode_on"
app:title="@string/title_night_mode" />
</PreferenceScreen>
这些代码行对我有用
注意:
AppTheme
应该扩展自Theme.AppCompat.DayNight
或Theme.MaterialComponents.DayNight
,如果您想依靠平台来实现夜间主题。否则,您可以分别创建从主题.MaterialComponents.Light
和主题扩展而来的白天
和夜晚
主题(AppCompat
也有类似的主题集)
MyApplication
类中检查首选项并设置主题,如下所示:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean nightMode = sharedPreferences.getBoolean(getString(R.string.key_night_mode), false);
if (nightMode)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
SharedReferences SharedReferences=PreferenceManager.GetDefaultSharedReferences(此);
boolean nightMode=SharedReferences.getBoolean(getString(R.string.key\u night\u mode),false);
if(夜间模式)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE\u NIGHT\u YES);
否则{
如果(Build.VERSION.SDK_INT将其放入您的首选项.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:defaultValue="false"
app:key="@string/key_night_mode"
app:summaryOff="@string/summary_night_mode_off"
app:summaryOn="@string/summary_night_mode_on"
app:title="@string/title_night_mode" />
</PreferenceScreen>
这些代码行对我有用
注意:
AppTheme
应该从Theme.AppCompat.DayNight
或Theme.MaterialComponents.DayNight
扩展,如果您想依赖于平台来创建夜间主题。否则,您可以创建单独的Day
和night
主题,该主题从Theme.MaterialComponents.Light
扩展而来Theme.MaterialComponents分别是(AppCompat
也有类似的主题集)
每次启动应用程序时,它都需要尊重用户的偏好,并相应地在用户设置的主题中启动。为此,您可以在MyApplication
类中检查偏好并设置主题,如下所示:
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean nightMode = sharedPreferences.getBoolean(getString(R.string.key_night_mode), false);
if (nightMode)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
else {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
else
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
}
SharedReferences SharedReferences=PreferenceManager.GetDefaultSharedReferences(此);
boolean nightMode=SharedReferences.getBoolean(getString(R.string.key\u night\u mode),false);
if(夜间模式)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE\u NIGHT\u YES);
否则{
if(Build.VERSION.SDK_INT)非常感谢您提示在应用程序中设置用户首选模式!非常感谢您提示在应用程序中设置用户首选模式!