Android的工作室 - 我怎样才能让交换机重新启动应用程序后进行检查 [英] Android Studio - How can I let Switch be checked after restarting the app
问题描述
我只对我的片段的切换开关。
什么是应用程序现在做的:用户打开应用程序,并在导航抽屉选择笔记本电脑,然后他看见一个开关(这是一个滑,它可以移动到打开或关闭......希望你明白我意思)。用户将开关至ON然后关闭该应用程序并重新打开它和开关是关闭...但它不应该是关闭...它应该是,喜欢的用户设置它
我该怎么办呢?该开关应保存其状态,用户如何选择它
我需要的MySQL或东西吗?
I only have a switch toggle on my fragment. What is the app doing now: The user opens the app and selects "Laptop" over the navigation drawer and then he sees a switch (it is a "slider" which you can move to on or off...hope you understand what I mean). The user sets switch to ON then closes the app and reopens it and the switch is off...but it shouldn't be off...it should be on, like the user sets it How can I do that? The switch should save its state, how the user selects it Do I need mysql or something?
我希望你明白我的意思。
I hope you understand what I mean
感谢
推荐答案
您应该使用共享preferences像这样:
you should use SharedPreferences like so:
public static final String SWITCH_PREFS = "Switcher";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
Switch switcher = (Switch)findViewById(R.id.yourSwitchId);
switcher.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(yourContext).edit();
editor.putBoolean(SWITCH_PREFS,isChecked);
editor.commit();
}
});
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(yourContext);
if(prefs.contains(SWITCH_PREFS) && switcher != null)
{
switcher.setChecked(prefs.getBoolean(SWITCH_PREFS,false));
}
}
没尝试这个code,但希望这个作品。
Didn't try this code, but hope this works.
这篇关于Android的工作室 - 我怎样才能让交换机重新启动应用程序后进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!