You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android切换深浅模式时如何保留TextView显示的count变量计数状态

问题根因

切换深浅模式时,Android系统默认会销毁重建当前Activity,你的count是类成员临时变量,Activity重建后会被重新初始化为0,就出现了计数归零的问题。

解决方案

你已经在项目中引入了SharedPreferences存储深色模式状态,直接复用它存储count值即可,修改点如下:

  • 点击按钮更新count时,同步将最新count值存入SharedPreferences
  • Activity重建(onCreate执行)时,读取SharedPreferences中存储的count值,赋值给本地变量同时更新TextView显示

修改后的完整代码

public class MainActivity extends AppCompatActivity {

    Button button;
    TextView textView1,textView2;
    Switch aSwitch;
    int count=0;
    String light="light",dark="dark";
    RelativeLayout relativeLayout;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        textView1 = findViewById(R.id.remindmetext);
        textView2 = findViewById(R.id.countext);
        aSwitch = findViewById(R.id.switch1);
        relativeLayout = findViewById(R.id.relative);

        // 初始化SharedPreferences
        sharedPreferences = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        boolean isDarkModeOn = sharedPreferences.getBoolean("isDarkModeOn", false);
        // 读取存储的count值,默认0
        count = sharedPreferences.getInt("click_count", 0);
        // 初始化显示count
        textView2.setText(Integer.toString(count));

        if (isDarkModeOn) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            aSwitch.setText("Light");
        } else {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            aSwitch.setText("Dark");
        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count=count+1;
                textView2.setText(Integer.toString(count));
                // 保存最新count到SP
                editor.putInt("click_count", count);
                editor.apply();
            }
        });

        aSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isDarkModeOn) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    editor.putBoolean("isDarkModeOn", false);
                    editor.apply();
                    aSwitch.setText("Dark");
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    editor.putBoolean("isDarkModeOn", true);
                    editor.apply();
                    aSwitch.setText("Light");
                }
            }
        });
    }
}

可选临时存储方案

如果你不需要下次打开APP还保留历史计数,仅需要切换深浅模式时保留状态,可以重写onSaveInstanceState方法存储count值,在onCreate中从savedInstanceState参数中读取恢复即可,不需要持久化写入SP。

内容的提问来源于stack exchange,提问作者Aviroxi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 13:24:07